CodesJava

Easy learning with example program codes

Reflect.set() JavaScript JS

The JavaScript Reflect.set() method sets the value of the property of an object.

Syntax:

Reflect.set(target, propertyKey, value[, receiver])

Parameters:
target: It represents the object on which to set the property.
propertyKey: It represents the name of the property to set.
value: It represents the value to set corresponding to specified property.
receiver: It represents value of this provided for the call to target if a setter is encountered. It is optional parameter.

Return:
It returns true if the property is successfully set otherwise returns false.

Note: It throws TypeError, if target is not an Object.

Example 1:

<!DOCTYPE html>
<html>
<body>
<script>  
const obj = [];  
Reflect.set(obj, 0, 'alpha');
Reflect.set(obj, 1, 'beta');
Reflect.set(obj, 2, 'gamma');
document.write(obj);   
</script>
</body>
</html>

Output:

alpha,beta,gamma

Example 2:

<!DOCTYPE html>
<html>
<body>
<script>
 
const object1 = {};
Reflect.set(object1, 'property1', 456);
 
document.write(object1.property1);
document.write("</br>");
 
const array1 = ['codesjava', 'codesjava', 'codesjava'];
Reflect.set(array1, 2, 'jai');
 
document.write(array1[2]);
</script>
</body>
</html>

Output:

456
jai
Please follow and like us:
error








Copyright © 2019 CodesJava DMCA.com Protection Status