Quick Overview
Primitives copy by value and objects copy by reference, so `b = a` duplicates a number but shares an object. That single distinction explains most surprise mutations in a codebase.
Every piece of information in JavaScript is either a Primitive or an Object. Think of primitive data as simple, atomic values stored directly in memory (like a single number or a word), while objects are complex collections of related data stored by reference.
Understanding primitives prevents unexpected bugs when copying or comparing values. In JavaScript, primitives are immutable (their inner value cannot be changed directly) and are always copied by value. When you assign a primitive to a new variable, JavaScript creates a fresh, independent copy in memory.
JavaScript features exactly 7 primitive types:
number: Whole numbers and decimals like 42, 3.14, and special values like NaN (Not-a-Number).string: Text enclosed in quotes like "Hello World" or 'JavaScript'.boolean: Logical states: true or false.undefined: Memory space allocated by the engine, but no value assigned yet.null: Intentional assignment representing "no value" or an empty object pointer.symbol: Unique, immutable identifier created via Symbol("id").bigint: Arbitrary precision integers for handling numbers larger than 2^53 - 1 (e.g. 9007199254740991n).You can check any value's type using the built-in typeof operator:
const score = 100;
console.log(typeof score); // "number"
const name = "DevLoom";
console.log(typeof name); // "string"
const isActive = true;
console.log(typeof isActive); // "boolean"
Primitives are stored directly by value on the stack memory layout:
[ Stack Memory ]
┌──────────────┬─────────────────┐
│ Variable │ Stored Value │
├──────────────┼─────────────────┤
│ age │ 25 │
│ name │ "Alex" │
│ isOnline │ true │
└──────────────┴─────────────────┘
When you copy a primitive (let b = a), a completely new memory cell is allocated with the copied value. Mutating b has zero effect on a.
typeof null Historical Bug: An infamous quirk from JavaScript's creation in 1995 makes typeof null return "object" instead of "null". This is a legacy bug that cannot be fixed without breaking existing websites. Always check for null using strict equality: value === null.name.toUpperCase() does NOT modify the original string name. It returns a brand new string!Write it yourself. The tests below run against your code.
Primitives form the raw building blocks of all JavaScript programs. Next, let's explore how variables act as labeled containers for these primitive values!
`const` prevents reassignment, not mutation: a `const` object's properties can still be changed. `let` is scoped to its block, while `var` is function-scoped and leaks out of loops.
Only backticks interpolate: `${user}` reads the variable, while the identical text in quotes prints literally. Strings are immutable, so every method returns a new one rather than editing in place.
Every JavaScript number is a 64-bit float, which is why `0.1 + 0.2` comes out as `0.30000000000000004`. `%` returns a remainder that keeps the sign of the left operand, so `-7 % 3` is `-1`.
Average 5.0 by 1 learner