Loading topic
string, number, bigint, boolean, undefined, null, and symbol. The symbol type was added in ES2015. A symbol value is guaranteed to be unique. This chapter explains how to create symbols, what that guarantee means, and how symbols differ from the 2 min readtypeof, template literals, and the basics of type coercion. It also builds on the previous chapter: every call to Symbol() returns a new, unique value.2 min readSymbol() and that every call returns a unique value. It also assumes you are comfortable with computed property names ({ [expr]: value }) and with ES6 classes.2 min readObject.keys, for...in, and JSON.stringify on ordinary objects.2 min readSymbol() creates unique values, how symbols work as property keys, and how a symbol's description works. You should also be comfortable with ES modules or CommonJS require.2 min readSymbol.for. It also assumes you are comfortable with ES6 classes and for...of.2 min readfor...of loops, spread syntax, and the idea from the previous chapter that well-known symbols are hooks the language looks up on your objects. It also helps to have seen generator functions (function*), but they are explained briefly here.2 min readSymbol.iterator and the iteration protocol work. It also assumes you are comfortable with Promises, async/await, and generator functions. Here you will learn the async version of the protocol. In it, each value can arrive later, for example from a time2 min read+x, a + b, and template literals. It also assumes you have read the earlier chapter on well-known symbols. The focus here is one hook: Symbol.toPrimitive, which decides what an object turns into when the language n2 min readSymbol.toStringTag changes the label that Object.prototype.toString reports.2 min read[Symbol.iterator] define symbol-keyed methods. It covers the hook that lets an object decide what x instanceof Y returns.2 min readObject.getOwnPropertySymbols and Reflect.ownKeys. It shows why a symbol key hides data only from casual inspection. It then compares symbol keys with the two tools that actual2 min readWeakMap works with object keys, and how Symbol() differs from Symbol.for(), which earlier chapters covered. It explains which symbols you can use as weak keys and why registered symbols are rejected.2 min readSymbol.for. It shows where symbols work well in everyday code and where they tend to cause bugs.2 min read