Open In App

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:

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.




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 = ['geeks1', 'geeks2', 'geeks3', 'geeks4'];
console.log(Reflect.get(array1, 3));

Output
1
true
true
false
geeks4

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




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

Output
geeksforgeeks
false
true

Supported Browsers:

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

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


Article Tags :