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 of the set corresponding to the specified property.
receiver: It represents the value of this provided for the call to target if a setter is encountered. It is an optional parameter.
Return:
It returns true if the property is successfully set otherwise returns false.
Note: It throws TypeError, if the 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>
Example 2:
<!DOCTYPE html> <html> <body> <script> const object1 = {}; Reflect.set(object1, 'property1', 456); document.write(object1.property1); document.write("</br>"); const array1 = ['w3schools', 'w3schools', 'w3schools']; Reflect.set(array1, 2, 'jai'); document.write(array1[2]); </script> </body> </html>