Skip to content
Related Articles

Related Articles

JavaScript | Symbol.unscopables Properties
  • Last Updated : 19 Jun, 2020

The Symbol.unscopables property in Javascript is a well-known symbol that is used to specify an object value of whose own and inherited property names are excluded from the environment bindings.

Syntax:

object[Symbol.unscopables]

Property attributes: This property holds an Object and it is not Writable, Enumerable and Configurable.

Return value: Check the variable appear in lexical scope variable.

Below examples illustrate the Symbol.unscopables properties in JavaScript:



Example 1: If all property are set to false.




// JavaScript to illustrate Symbol.toPrimitive  
var obj1 = {   
  val: "Have",   
  val1: "FUN"
};  
obj1[Symbol.unscopables] = {     
  val1: false,      
  val: false   
};  
with (obj1) {  
  console.log(val1);  
}  
with (obj1) {  
  console.log(val);  
}

Output:

> "FUN"
> "Have"

Example 2: If any property are set to true.




// JavaScript to illustrate Symbol.toPrimitive  
var obj1 = {   
  val: "Have",   
  val1: "FUN"
};  
obj1[Symbol.unscopables] = {     
  val1: false,      
  val: true   
};  
with (obj1) {  
  console.log(val1);  
}  
with (obj1) {  
  console.log(val);  
}

Output:

"FUN"
Error: val is not defined

Example 3:




var list = [];
  
with (Array.prototype) {
  list.push('unscopables');
}
  
console.log(Object.keys(Array.prototype[Symbol.unscopables])); 

Output:

[
  'copyWithin', 'entries',
  'fill',       'find',
  'findIndex',  'flat',
  'flatMap',    'includes',
  'keys',       'values'
]

Supported Browsers: The browsers supported by JavaScript Symbol.unscopables properties are listed below:

  • Google Chrome 51
  • Firefox 50
  • Edge 15
  • Opera
  • Apple Safari

Reference: https://devdocs.io/javascript/global_objects/symbol/unscopables

full-stack-img

My Personal Notes arrow_drop_up
Recommended Articles
Page :