Chapter II · Variables & Values

Primitive Values

Primitive values are basic, immutable data types with no methods or properties of their own.

01 What are Primitive Values?

JavaScript has seven primitive types: string, number, bigint, boolean, undefined, symbol, and null.

All primitives are immutable — once created, their actual value cannot be altered. When you perform operations on primitives, JavaScript returns a brand new value rather than modifying the original.

02 Simple Example

Demonstrating primitive immutability and string methods.

primitives.js
let str = "hello";
str.toUpperCase(); // Returns "HELLO", but str is unchanged!
console.log(str); // "hello"

str = str.toUpperCase(); // Reassignment updates the variable pointer
console.log(str); // "HELLO"

Calling toUpperCase() does not mutate the original string in memory; it produces a new string value.

03 How It Works

Primitives are stored directly on the call stack or inline within execution context environments, making value lookups extremely fast.

Variable Reference Primitive Value

Primitive values are stored directly by value, ensuring complete immutability.

04 Step-by-Step

  1. A primitive value is created and stored in memory.

  2. A variable points directly to that primitive value.

  3. When modified or transformed, a new primitive value is generated in memory.

  4. The variable pointer is updated to reference the new value.

05 Mental Model

"A carved stone tablet where letters cannot be erased, only replaced by carving a brand new tablet."

You cannot change what is already written; you can only create a new version and point to that instead.

06 Common Mistakes

Confusing "primitive" with "stored on the stack." "Stack vs. heap" is a useful teaching simplification, but the ECMAScript specification never mandates where a value physically lives in memory — that's an engine implementation detail. What the spec does guarantee is that primitives are compared and copied by value, not by reference. That behavioral guarantee is what actually matters for writing correct code.

Best practice: treat typeof null === "object" as a well-known historical bug in the language (kept for backward compatibility), not a design choice to imitate — use value === null to test for null specifically.

07 Interview Questions

Q: Are strings mutable in JavaScript?

No — string primitives are immutable. Methods like .toUpperCase() return a new string rather than modifying the original.

Q: How many primitive types does JavaScript have?

Seven: string, number, boolean, null, undefined, symbol, and bigint.

Key Takeaway

Primitive values are immutable, light data types passed by value rather than reference across your program.

Related Concepts