Saturday, October 5, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Symbol unscopables Property

JavaScript Symbol unscopables Property

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 appears in the lexical scope variable. 

The below examples illustrate the Symbol.unscopables properties in JavaScript:

Example 1:In this example, we will see the output If all property are set to false. 

javascript




// JavaScript to illustrate Symbol.toPrimitive 
let 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: In this example, we will see the output If any property is set to true. 

javascript




// JavaScript to illustrate Symbol.toPrimitive 
let 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: In this example, we will see all the unscopables

javascript




let 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 are supported by JavaScript Symbol.unscopables properties are listed below:

  • Google Chrome 45
  • Edge 12 and above
  • Firefox 48 and above
  • Opera 32 and above
  • Safari 9 and above

We have a complete list of Javascript symbols, to check those please go through the Javascript Symbol Complete 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