Gå till:

Våra volontärer har inte översatt denna artikel till Svenska ännu. Gå med oss och hjälpa till att få jobbet gjort!
Du kan också läsa artikeln på English (US).

In JavaScript, Symbol is a primitive value.

A value having the data type "symbol" can be referred to as a "symbol value."  In the JavaScript run-time environment, a symbol value is created by invoking the function Symbol, which dynamically produces an anonymous, unique value. A symbol may be used as an object property. 

Symbol can have an optional description, but for debugging purposes only.

Symbol type is a new feature in ECMAScript 2015 and there is no ECMAScript 5 equivalent for symbol. In some programming languages the symbol data type is referred to as an "atom."

Well-known symbols

The Symbol class has constants for so-called well-known symbols. These symbols let you configure how JS treats an object, by using them as property keys. Examples of well-known symbols are: Symbol.iterator for array-like objects, or Symbol.search for string objects.

Global symbol registry

The methods that access the global symbol registry are Symbol.for() and Symbol.keyFor(); these mediate between the global symbol table (or "registry") and the run-time environment. The symbol registry is mostly built by JavaScript's compiler infrastructure, and the symbol registry's content is not available to JavaScript's run-time infrastructure, except through these reflective methods. The method Symbol.for("tokenString") returns a symbol value from the registry, and Symbol.keyFor(symbolValue) returns a token string from the registry; each is the other's inverse, so the following is true:

Symbol.keyFor(Symbol.for("tokenString")) == "tokenString"; // true

Learn more

General knowledge