logo

Data Types

Primitive and reference data types in JavaScript and how they behave.

Short explanation

JavaScript has primitive types (string, number, boolean, null, undefined, symbol, bigint) and reference types (objects, arrays, functions). Primitives are immutable values, while objects are references stored on the heap.

Syntax example

const name = 'Alice'; // string
const count = 10; // number
const user = { name: 'Alice' }; // object

How JS handles it internally

Primitives are stored directly in memory slots (or registers) whereas objects are allocated on the heap with references pointing to them. Assignment of objects copies the reference, not the object itself.

FAQ

Q: Why does comparing objects with === fail even if they look the same?

A: === compares references for objects, so two different object instances with identical properties are not equal.