Symbol type

2 snippets across 2 stacks - JavaScript, TypeScript

JSJavaScript

Symbols

JS · Data Types
Syntax
const sym = Symbol(description);
Example
const STATUS = Symbol("status");
const order = { [STATUS]: "shipped", id: 101 };

console.log(order[STATUS]);       // "shipped"
console.log(Object.keys(order));  // ["id"]

Note Symbols are unique and hidden from normal enumeration. Use Symbol.for("key") to create shared/global symbols across modules.

TSTypeScript

symbol & bigint

TS · Basic Types
Syntax
let varName: symbol = Symbol(description);
let varName: bigint = valueBigInt;
Example
const uniqueKey: unique symbol = Symbol("cacheKey");
let regularSym: symbol = Symbol("temp");

let hugeNumber: bigint = 9007199254740993n;
let anotherBig: bigint = BigInt("123456789012345678");
Output
// unique symbol creates a distinct type; bigint handles arbitrarily large integers

Note unique symbol can only be used with const declarations and creates a specific subtype of symbol. bigint cannot be mixed with number in arithmetic - you must explicitly convert. bigint requires target ES2020 or later in tsconfig.

Frequently asked questions

How does JavaScript handle symbol type?
This task is covered in 2 stacks on this page: JavaScript, TypeScript. The "Symbols" snippet in JavaScript uses `const sym = Symbol(description);`.
Which code does the JavaScript example use?
The "Symbols" snippet uses `const sym = Symbol(description);`, from the Data Types section of the JavaScript cheat sheet.
Which stacks cover "symbol type" on this page?
JavaScript, TypeScript. Together they hold 2 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Symbols": Symbols are unique and hidden from normal enumeration. Use Symbol.for("key") to create shared/global symbols across modules.