set() - JavaScript Tutorial

set()

BLUF: This tutorial on set() provides an in-depth look at JavaScript's core features. It includes practical examples and code snippets to help you master modern JS development.
Key Discovery: set()

Understanding set() is crucial for building dynamic, interactive web applications. Explore the examples below to see it in action.

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:

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience