By the end of this lesson, you can identify JavaScript’s primitive and reference data types, explain how they are stored and compared, and use typeof to inspect values safely.
What it is
JavaScript values fall into two broad groups: primitive types and reference types. A primitive value is a simple, immutable value. When you assign it to another variable, JavaScript copies the value. A reference value is an object-like value, such as an object, array, or function. When you assign it, JavaScript copies the reference, not the underlying data.
The primitive types are string, number, bigint, boolean, undefined, and symbol. null is a primitive value, but typeof null returns "object" because of an early implementation quirk. Related terms include immutability, reference, identity, and type coercion.
Why it matters
- It explains why changing one object can affect another variable that points to the same object.
- It helps you choose the right equality check for values and objects.
- It prevents bugs when copying arrays or objects.
- It makes debugging easier by clarifying what
typeofcan and cannot tell you. - It supports safer function design, especially when passing objects as arguments.
Syntax or steps
The smallest useful pattern is to inspect a value with typeof, then handle objects separately.
- Declare or receive a value.
- Use
typeof valueto get a type string. - If the result is
"object", check whether the value isnull, an array, or a plain object. - Compare primitives with
===; compare references by identity.
Example
const primitive = true;
const number = 42;
const text = "hello";
const missing = undefined;
const empty = null;
const object = { name: "Ada" };
const array = [1, 2, 3];
const functionValue = function () {};
console.log(typeof primitive); // "boolean"
console.log(typeof number); // "number"
console.log(typeof text); // "string"
console.log(typeof missing); // "undefined"
console.log(typeof empty); // "object"
console.log(typeof object); // "object"
console.log(typeof array); // "object"
console.log(typeof functionValue); // "function"
const a = { value: 1 };
const b = a;
const c = { value: 1 };
console.log(a === b); // true
console.log(a === c); // false
The first block creates several values and prints their types. Notice that null and arrays both report "object", while functions report "function". The second block shows reference behavior: b receives the same reference as a, so a === b is true. c is a different object with the same contents, so a === c is false.
Common mistakes
- Assuming
typeof nullmeans null is an object. Fix: testvalue === nullbefore treating it as an object. - Copying an object with
=. Fix: create a new object with spread syntax, such as{ ...original }, or use a deep copy when nested data must be independent. - Using
==for type checks. Fix: prefer===to avoid coercion surprises. - Thinking arrays are a separate
typeofresult. Fix: useArray.isArray(value)for arrays.
When to use it
Use primitive types for simple values and reference types for structured data. The table below compares the closest alternatives.
| Kind | Examples | Comparison | Best for |
|---|---|---|---|
| Primitive | 42, "hi", true | By value | Flags, IDs, text, numbers |
| Reference | {}, [], functions | By identity | Collections, state, behavior |
Practice
Guided exercise: Create one primitive, one object, and one array. Print each with typeof, then use Array.isArray on the array. Expected output includes "string", "object", "object", and true.
Challenge: Write code where two variables point to the same object, change one property, and prove the other variable sees the change. Hint: use const user = { name: "Sam" }; and const copy = user;.
Quick check
Question: What does typeof [] return, and what is a safer array check?
Answer: It returns "object". Use Array.isArray([]) to check for arrays.
Summary
JavaScript separates simple immutable primitives from mutable reference values. Primitives are copied and compared by value, while objects, arrays, and functions are copied by reference and compared by identity. Knowing this distinction prevents many common bugs.