JavaScript | Reflect.get() Method

The 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 accept 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 exception given as the result, when the target is not an Object.



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

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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));

chevron_right


Output:

1
true
true
false
"geeks4"

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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  
); 

chevron_right


Output:

"geeksforgeeks"
false
true

Supported Browsers: The browsers supported by JavaScript Reflect.get() Method are listed below:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari
  • Edge

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.