The Reflect.set method is utilized to assign a new value to a property of an object. Upon successful assignment, it returns true; otherwise, it returns false.
Syntax:
Example
Reflect.set(obj, Key, value[, receiver])
Parameters:
TargetObj: This refers to the object where the property will be assigned.
Key : It is the name of the property to set.
value : It is the value to set.
Return Value: When a setter is encountered, the receiver is the value passed to the target during the call.
Return value:
The function returns a Boolean value that signifies if the property was set successfully or not.
Exceptions:
A TypeError, if the target is not an Object.
Browser Support:
| Chrome | 49 |
|---|---|
Edge |
12 |
| Firefox | 42 |
| Opera | 36 |
Example 1
Example
const array1 = [];
Reflect.set(array1, 2, 'gosse');
console.log(array1[2]);
Output:
Output
"gosse"
Example 2
Example
const obj = {};
Reflect.set(obj, 'pro', 32);
console.log(obj.pro);
Output:
Example 3
Example
const n={};
const m={};
Reflect.set(n,'ptou',7);
console.log(n.ptou);
Reflect.set(m,'too',4);
console.log(m.too);
Output: