BigInt and Symbol

Bigint and Symbol
πŸ‘¨β€πŸ’Ό TypeScript has two more primitive types that are less commonly used but good to know about: bigint and symbol.

BigInt - Large Integers

Regular number has limitsβ€”it can't accurately represent integers larger than Number.MAX_SAFE_INTEGER (about 9 quadrillion). bigint handles arbitrarily large integers:
const big: bigint = 9007199254740993n // Note the 'n' suffix
const alsoBig: bigint = BigInt('9007199254740993')

// BigInt arithmetic
const sum = 1000000000000000000n + 1n // Works correctly!
BigInt and number don't mix directly. You can't do 5n + 3β€”you need to convert one type to the other.

Symbol - Unique Identifiers

Symbols are guaranteed-unique values, useful as object keys when you need to avoid name collisions:
const id: symbol = Symbol('userId')
const anotherId: symbol = Symbol('userId')

id === anotherId // false - each Symbol() creates a unique value!
Symbols are often used for "hidden" object properties or library-internal keys.
🐨 Open
index.ts
and:
  1. Create a bigint literal with the n suffix
  2. Perform arithmetic with bigint values
  3. Create symbols and observe their uniqueness
πŸ’° BigInt literals use the n suffix.
πŸ“œ MDN - BigInt πŸ“œ MDN - Symbol

Please set the playground first

Loading "BigInt and Symbol"
Loading "BigInt and Symbol"
Login to get access to the exclusive discord channel.
Loading Discord Posts