Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Standard built-in objects
  5. Symbol
  6. Symbol()

Symbol() constructor

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨September 2015⁩.

TheSymbol() function returns primitive values of type Symbol.

Try it

const symbol1 = Symbol();const symbol2 = Symbol(42);const symbol3 = Symbol("foo");console.log(typeof symbol1);// Expected output: "symbol"console.log(symbol2 === 42);// Expected output: falseconsole.log(symbol3.toString());// Expected output: "Symbol(foo)"console.log(Symbol("foo") === Symbol("foo"));// Expected output: false

Syntax

js
Symbol()Symbol(description)

Note:Symbol() can only be called withoutnew. Attempting to construct it withnew throws aTypeError.

Parameters

descriptionOptional

A string. A description of the symbol which can be used for debugging but not toaccess the symbol itself.

Examples

Creating symbols

To create a new primitive symbol, you writeSymbol() with an optionalstring as its description:

js
const sym1 = Symbol();const sym2 = Symbol("foo");const sym3 = Symbol("foo");

The above code creates three new symbols. Note thatSymbol("foo") does notcoerce the string"foo" into a symbol. It creates a new symbol each time:

js
Symbol("foo") === Symbol("foo"); // false

new Symbol()

The following syntax with thenew operator will throw aTypeError:

js
const sym = new Symbol(); // TypeError

This prevents authors from creating an explicitSymbol wrapper objectinstead of a new symbol value and might be surprising as creating explicit wrapperobjects around primitive data types is generally possible (for example,new Boolean,new String andnew Number).

If you really want to create aSymbol wrapper object, you can use theObject() function:

js
const sym = Symbol("foo");const symObj = Object(sym);typeof sym; // "symbol"typeof symObj; // "object"

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-symbol-constructor

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp