Symbol is a unique identifier in JavaScript. It is used as an object attribute name to prevent naming conflicts. It is unique, immutable and private. It can be created and assigned through Symbol() and is only strictly equal to itself. Compare.
Symbol in JavaScript
Symbol is a primitive data type in JavaScript that represents a unique identifier . It was first introduced in the ES6 (ECMAScript 2015) release.
Usage:
Symbol is mainly used to create unique property names to avoid naming conflicts and enhance code readability.
Characteristics:
for...in
loops. Create Symbol:
Use Symbol()
function to create Symbol:
<code class="js">const mySymbol = Symbol();</code>
Compare Symbol:
Since Symbols are unique, they can only be strictly equal to themselves (===
) Comparison:
<code class="js">console.log(mySymbol === mySymbol); // true console.log(mySymbol === Symbol()); // false</code>
As Property name:
Symbol can be used as the name of an object's property, thus freeing it from naming conflicts:
<code class="js">const person = { [Symbol("name")]: "John Doe", age: 30 }; console.log(person[Symbol("name")]); // "John Doe"</code>
Other usage:
Symbol has other uses, including:
The above is the detailed content of What does symbol mean in js. For more information, please follow other related articles on the PHP Chinese website!