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.