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 largeNumber as the bigint 9007199254740993n
  2. Create anotherLarge as the bigint 1000000000000000000n
  3. Create sum by adding those two bigints
  4. Create userId with Symbol('user-id')
  5. Create anotherId with the same description Symbol('user-id')
  6. Create areEqual by comparing userId === anotherId, then inspect the result
  7. Export all six names

Required exports

largeNumber, anotherLarge, sum, userId, anotherId, areEqual

Completion criteria

  • largeNumber is 9007199254740993n (typeof "bigint")
  • anotherLarge is 1000000000000000000n
  • sum equals the sum of those two bigints
  • userId and anotherId are both symbols
  • areEqual reflects whether those two symbols compare equal with ===
πŸ’° 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