FSM Full Stack Masterclass
Platform under construction
JavaScript course badge

Values & Types

Intermediate

Symbol & BigInt

Symbol and BigInt are primitive types for special jobs: unique property keys and integer values beyond the safe Number range.

const id = Symbol("id");
const huge = 9007199254740993n;

console.log(typeof id);   // "symbol"
console.log(typeof huge); // "bigint"

Values & Types

Symbol and BigInt are not everyday beginner tools, but they solve real problems.

Symbol creates unique primitive values. Even two symbols with the same description are different. Symbols are often used as unique object property keys or advanced protocol hooks.

BigInt represents large integers beyond the safe integer range of Number. It is useful for identifiers, counters and numeric domains where integer precision matters.

Both types have sharp edges. Symbols do not convert to strings implicitly like ordinary values. BigInt cannot be freely mixed with Number arithmetic and does not serialize to JSON by default.

Symbol()

Creates a unique symbol every time, even with the same description.

Symbol.for()

Uses the global symbol registry and can return the same shared symbol for a key.

BigInt literal

Append n to an integer literal, such as 10n.

BigInt limits

No fractions, no Math methods and no mixed arithmetic with Number.

Examples

Good type handling makes JavaScript predictable.

Use each type for its real purpose

const internalId = Symbol("internal-id");

const user = {
  name: "User A",
  [internalId]: "private-ish-key",
};

const chainHeight = 9007199254740993n;
console.log(typeof chainHeight); // "bigint"

Mixing BigInt and Number arithmetic

const big = 10n;
const normal = 5;

console.log(big + normal);
// TypeError: cannot mix BigInt and Number arithmetic

Rules that matter

Types are not paperwork. They are behavior.

JavaScript lets values move flexibly, but professional code keeps meaning clear. Convert at boundaries, validate input and choose values that represent the real idea.

Use Symbol for uniqueness

Descriptions are labels for debugging, not identity.

Use Symbol.for only when sharing is intended

The global registry creates shared symbols by key.

Use BigInt for large integers

Do not use BigInt for decimal money or fractional measurements.

Do not mix numeric types blindly

Convert deliberately when moving between Number and BigInt.

Remember JSON limits

JSON.stringify throws on BigInt unless you provide a strategy.

Avoid BigInt for cryptography timing by default

BigInt operations are not guaranteed constant-time.

Production thinking

Data quality starts with value quality.

Why does this matter?

These types are not daily sugar. They exist because normal strings, numbers and object keys are not enough for every problem.

Accessibility

Symbol and BigInt rarely affect accessibility directly, but bugs in identifiers or counters can still break UI state and data display.

Production note

Production systems must define how BigInt crosses JSON, database and API boundaries. Symbols should not be used where ordinary public keys are expected.

SEO note

Large numeric identifiers rendered incorrectly can break links, structured data or canonical references.

Live code lab

Change the HTML, CSS or JavaScript and run the result.

The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.

Mini assignment

Try this now.

  • Change Symbol() to Symbol.for() and compare equality.
  • Try adding a BigInt and a Number and read the error.
  • Convert the Number to BigInt before adding.

Practice assignment

Do this before moving to the next topic.

  1. Create two symbols with the same description and compare them.
  2. Create one BigInt with n syntax.
  3. Write down why JSON needs a BigInt strategy.

Try it yourself

Compare Symbol uniqueness and BigInt precision

Live preview

Self-check

Before you continue, prove that you understand Symbol & BigInt.

Intermediate

Say the answer in plain language. If you can explain the value behavior, the code becomes much easier to debug.

  1. Can you explain why two Symbol("id") calls are different?
  2. Can you explain when Symbol.for is different?
  3. Can you explain what BigInt solves?
  4. Can you explain why BigInt and Number cannot be mixed casually?
  5. Can you explain one BigInt production limitation?

Chapter checkpoint

Values & Types checkpoint

Finish this chapter by turning the lessons into a small practical proof.

Build

Create examples that compare strings, numbers, booleans, null, undefined and objects.

Deliverables

  • type examples
  • falsy/truthy table
  • input conversion example

Quality checks

  • external input is converted intentionally
  • null and undefined are not treated as the same by accident

Review question

Can you predict the type of every value before running the code?