Saturday, November 16, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Reflect get() Method

JavaScript Reflect get() Method

JavaScript Reflect.get() method in JavaScript is used to allow users to get the property from an object as a function. This method always returns the value of the property.

Syntax:

Reflect.get(target, propertyKey, receiver) 

Parameters: This method accepts three parameters as mentioned above and described below:

  • target: This parameter is used to get the property and it is the target object.
  • propertyKey: This parameter is used to get the name of the key.
  • receiver: It is an optional parameter and it is the value of this provided for the call to object if a getter is encountered.

Return value: This method always returns the value of the property.

Exceptions: A TypeError is an exception given as the result when the target is not an Object.

The below examples illustrate the Reflect.get() Method in JavaScript:

Example 1: In this example, we will get the property of the object using the Reflect.get() Method in JavaScript.

javascript




const object = {
    val1: 1,
    val2: 2
};
console.log(Reflect.get(object, 'val1'));
 
const abc = { val: 21 };
console.log(Reflect.get(abc, "val") === 21);
console.log(Reflect.get(abc, "x") === undefined);
console.log(Reflect.get(abc, "y") === 21);
 
const array1 = ['neveropen1', 'neveropen2', 'neveropen3', 'neveropen4'];
console.log(Reflect.get(array1, 3));


Output

1
true
true
false
neveropen4

Example 2: In this example, we will get the property of the object abc using the Reflect.get() Method in JavaScript.

javascript




let abc = { val: 1 };
 
let obj1 = new Proxy(abc, {
    get(t, k, r) {
        return k + 'for' + k
    }
})
console.log(Reflect.get(obj1, 'neveropen'));
 
const valx = { prop: 21 };
const valy = Object.create(valx);
console.log(
    Reflect.get(valy, "prop") === 12
);
console.log(
    Reflect.get(valy, "prop") === 21
);


Output

neveropen
false
true

Supported Browsers:

The browsers are supported by JavaScript Reflect.get() Methods are listed below:

  • Google Chrome 49 and above
  • Edge 12 and above
  • Firefox 42 and above
  • Opera 36 and above
  • Safari 10 and above

We have a complete list of Javascript Reflects methods, to check those go through the JavaScript Reflect Reference article.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments