Compile Ready
Module 2 · Variables & Data Types

Primitive Types

Beginner8m read3m practice11m total
PrimitivesTypesNumberImmutability

Introduction

JavaScript has seven primitive types: string, number, boolean, null, undefined, symbol, and bigint. A primitive is a value that is not an object and has no mutable internal properties.

Primitives are copied by value, compared by value, and behave as immutable values even when JavaScript temporarily boxes them to access methods like 'hello'.toUpperCase().

Why This Matters

Many interview traps come from type fundamentals: typeof NaN === 'number', 0.1 + 0.2 !== 0.3, strings cannot be mutated character-by-character, and primitives do not share state when assigned to another variable.

Theory

The seven primitives

PrimitiveExampleNotes
string'Ada'Immutable sequence of UTF-16 code units.
number42, 3.14, NaN, InfinityIEEE-754 double; safe integers only up to Number.MAX_SAFE_INTEGER.
booleantrue, falseOften produced by comparisons.
nullnullIntentional absence of an object/value.
undefinedundefinedMissing or uninitialised value.
symbolSymbol('id')Unique property key / protocol hook.
bigint123nArbitrary-precision integer.

Immutability

Primitive values cannot be changed in place. Operations produce new values. For example, strings feel array-like, but assigning to name[0] does not change the string.

Pass by value

When you assign a primitive to another variable or pass it into a function, JavaScript copies the primitive value. Reassigning the parameter or second variable does not affect the original.

Number interview classics

NaN is still a number type because it is a numeric sentinel for an invalid numeric result. Floating-point decimals are binary approximations, so 0.1 + 0.2 is 0.30000000000000004, not exactly 0.3. Use tolerances for decimal comparisons, and use bigint for very large integers when fractional values are not needed.

Visual Diagrams

Primitive assignment copies the value
let a = 10
let b = a

a -> 10
b -> 10

b = 11

a -> 10
b -> 11

Changing `b` does not affect `a` because primitives are copied by value.

Code Examples

Primitive values do not share mutation

The function receives a copy of the primitive value. Reassigning the parameter cannot change the caller's variable.

Loading…

Number quirks to know

number covers normal numbers, NaN, and infinities. Decimal arithmetic uses binary floating point.

Loading…

Output Prediction

Predict the output #1

javascript
1let word = 'cat';
2word[0] = 'b';
3console.log(word);
4
5let a = 10;
6let b = a;
7b = b + 1;
8console.log(a);
9console.log(b);
10
11console.log(typeof NaN);
12console.log(0.1 + 0.2 === 0.3);

Interview Questions

1List the primitive types in JavaScript and explain how they are passed.

The seven primitives are string, number, boolean, null, undefined, symbol, and bigint. They are immutable values and are assigned/passed by value. If a function parameter receives a primitive and reassigns it, the caller's original variable is unchanged.

Asked at:AppleAmazonMicrosoft

Follow-ups

  • Why is `typeof null` not `null`?
  • Why does `typeof NaN` return `number`?

Quiz

1. Which is NOT a JavaScript primitive type?

2. Why is `0.1 + 0.2 === 0.3` false?

Summary

  • JavaScript has seven primitives: string, number, boolean, null, undefined, symbol, and bigint.
  • Primitives are immutable and are copied by value on assignment or function call.
  • `NaN` is a number value; `typeof NaN` returns `number`.
  • Floating-point arithmetic can produce precision surprises such as `0.1 + 0.2 !== 0.3`.

Cheat Sheet

Primitives: string, number, boolean, null, undefined, symbol, bigint.

Properties: immutable, compared by value, copied by value.

Number gotchas: typeof NaN === 'number'; decimal precision errors; safe integers up to Number.MAX_SAFE_INTEGER.

Strings: immutable; methods return new strings.