JS: Symbol (class)

By Xah Lee. Date: . Last updated: .

(new in JS: ECMAScript 2015)

What is the Keyword “Symbol”

Symbol is the value of the property key "Symbol" of the Global Object .

console.log(globalThis["Symbol"] === Symbol);

Type

Type of Symbol is a function.

// type of Symbol
console.log( typeof Symbol === "function" );

Parent

Parent of Symbol is Function.prototype.

Reflect.getPrototypeOf ( Symbol ) === Function.prototype

Purpose

Purpose of Symbol is:

Symbol Constructor

Properties

Well-Known (Predefined) Symbols

Well-Known symbols are symbols predefined in JavaScript.

Well-known symbols are built-in Symbol values They are typically used as the keys of properties whose values serve as extension points. Unless otherwise specified, well-known symbols values are shared by all Code Realms.

Well-Known symbols are denoted with 2 at signs in front, like this: @@iterator

Symbol.iterator

the operator instanceof is now a method of function, with property key [Symbol.hasInstance]. someting like o instanceof f same as f[Symbol.hasInstance](o). this makes it extensible.

Symbol.unscopables

Symbol.match method for customizing str.match(obj).

Well-known Symbols

Symbol.hasInstance
A method that determines if a constructor object recognizes an object as one of the constructor's instances. Called by the semantics of the instanceof operator.
Symbol.isConcatSpreadable
A Boolean valued property that if true indicates that an object should be flattened to its array elements by Array.prototype.concat .
Symbol.iterator
A method that returns the default Iterator for an object. Called by the semantics of the for-of statement.
Symbol.match
A regular expression method that matches the regular expression against a string. Called by the String.prototype.match method.
Symbol.replace
A regular expression method that replaces matched substrings of a string. Called by the String.prototype.replace method.
Symbol.search
A regular expression method that returns the index within a string that matches the regular expression. Called by the String.prototype.search method.
Symbol.species
A function valued property that is the constructor function that is used to create derived objects.
Symbol.split
A regular expression method that splits a string at the indices that match the regular expression. Called by the String.prototype.split method.
Symbol.toPrimitive
A method that converts an object to a corresponding primitive value.
Symbol.toStringTag
A String valued property that is used in the creation of the default string description of an object. Accessed by the built-in method Object.prototype.toString .
Symbol.unscopables
An object valued property whose own property names are property names that are excluded from the with environment bindings of the associated object.

JavaScript. Symbol