Lesson

JS Data Types

10 min reading
Free Course

JavaScript Data Types: Primitives and Reference Types

JavaScript is dynamically typed, meaning variables hold values of any type without fixed type annotations. The language has seven primitive types and one reference category (object), which includes arrays, functions, dates, and maps.

The 7 Primitive Types vs. Objects

Primitive values are immutable and copied by value. Reference values (objects and arrays) are mutable and copied by memory reference.

  • String: Text inside quotes or backticks, such as "Ada".
  • Number: Integers and decimals up to 64-bit floating-point precision, plus NaN and Infinity.
  • BigInt: Arbitrary-size integers ending in n, such as 9007199254740993n.
  • Boolean: Logical true or false.
  • Undefined: A declared variable with no assigned value yet.
  • Null: An intentional empty or missing value assigned by your code.
  • Symbol: A unique, immutable identifier often used for private-like object keys.
  • Object: Key-value records, arrays, functions, Date, Map, and Set.

Value Copy vs. Reference Copy Example

Copying a primitive creates an independent copy, while copying an object variable points both names at the same object in memory.

// Primitives copy by value
let a = 10;
let b = a;
b = 20;
console.log("Primitives:", a, b); // 10, 20

// Objects copy by reference
const original = { theme: "light" };
const alias = original;
alias.theme = "dark";
console.log("Original mutated:", original.theme); // "dark"

// Shallow copy with spread prevents shared mutation
const safeCopy = { ...original, theme: "sepia" };
console.log("Safe copy vs original:", safeCopy.theme, original.theme);

Common Mistakes to Avoid

  • Trusting typeof null: Due to a legacy 1995 bug, typeof null returns "object". Check value === null explicitly.
  • Using typeof to detect arrays: typeof [1, 2] returns "object". Always use Array.isArray(value) instead.
  • Mutating shared object arguments: Changing a property on an object passed into a function alters the caller's object unless you clone it first.

Frequently Asked Questions

What is the difference between null and undefined in JavaScript?

undefined means a variable or property has not been assigned a value yet. null is a value you explicitly assign to show "no object" or "empty on purpose."

Why are JavaScript primitives called immutable?

Methods like "abc".toUpperCase() return a brand-new string "ABC" without altering the original string in memory.

How do I make a deep copy of a nested object?

Modern browsers and Node.js support structuredClone(obj), which clones nested objects, arrays, and Date values cleanly.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
JavaScript Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum