JavaScript Data Types: Primitives, Reference Types & Memory Architecture

JavaScript is a dynamically-typed language (also called loosely typed). This means variable declarations are not bound to a fixed data type; instead, types are associated with the runtime values themselves, and a single variable can dynamically change its data type over time.

JavaScript Data Type Hierarchy: Primitives vs Reference Types

Figure 2: Memory Hierarchy showing 7 Primitive types stored in Call Stack vs Reference Types stored in Memory Heap.

The 7 Primitive Data Types (Passed by Value)

Primitives are fundamental, immutable building blocks stored directly in the execution stack:

Data Type Description & Range Code Example & typeof Output
Number 64-bit IEEE 754 floating-point numbers (integers & decimals). Includes NaN and Infinity. let n = 42; let pi = 3.14; typeof n === "number"
String Sequence of 16-bit Unicode characters enclosed in single quotes, double quotes, or backticks. let s = "Veewom"; typeof s === "string"
Boolean Logical binary values representing truth conditions: true or false. let active = true; typeof active === "boolean"
Undefined Default value of variables that have been declared but never assigned a value. let x; typeof x === "undefined"
Null Intentional absence of any object value. let user = null; typeof user === "object" (Historical Bug)
BigInt (ES2020) Arbitrary-precision integers exceeding Number's safe limit (2^53 - 1). Suffix with n. let big = 9007199254740995n; typeof big === "bigint"
Symbol (ES6) Unique, guaranteed immutable identifier primarily used as hidden object property keys. let id = Symbol("userId"); typeof id === "symbol"

Reference Types: Objects, Arrays & Functions

Everything in JavaScript that is not a primitive is an Object. Reference types can store collections of key-value pairs or complex executable code entities:

  • Standard Objects: let car = { brand: "Tesla", year: 2026 }; (Key-value dictionary).
  • Arrays: let marks = [98, 85, 92]; (Zero-indexed ordered list of elements).
  • Functions: First-class callable objects that can be stored in variables and passed as arguments.
  • Specialized Built-in Objects: Date, RegExp, Map, Set, Promise, Error.

Memory Model: Stack vs. Heap (Pass by Value vs. Reference)

How data is duplicated during assignment represents one of the most critical concepts in software architecture:

// 1. PRIMITIVES: Stored in Stack (Passed by Value / Deep Copy)
let a = 10;
let b = a; // 'b' receives a completely independent copy of value 10
b = 50;    // Changing 'b' does NOT affect 'a'
console.log("a:", a, "b:", b); // a: 10, b: 50

// 2. REFERENCE TYPES: Stored in Heap (Passed by Reference Pointer)
let obj1 = { name: "Veewom", rank: 1 };
let obj2 = obj1; // 'obj2' receives a COPY OF THE MEMORY POINTER (pointing to same Heap address)

obj2.rank = 5; // Modifying obj2 MUTATES the same underlying object in the Heap!
console.log("obj1.rank:", obj1.rank); // Output: 5! Both reflect change.

// Solution: Create a shallow clone or deep clone:
let independentClone = { ...obj1 }; // Spread syntax shallow copy
let deepClone = structuredClone(obj1); // Modern ES2022 deep clone API

University Exam Corner (BCA / MCA / B.Tech)

Exam Question: "What is NaN in JavaScript and what is its data type? How do you reliably check for NaN?"

Answer: NaN stands for "Not-a-Number". Paradoxically, typeof NaN === "number"! It represents an unrepresentable or failed mathematical operation (e.g., "apple" * 5).
Crucially, NaN === NaN is FALSE (it is the only value in JavaScript not equal to itself!).
To check reliably, use Number.isNaN(value) (ES6) rather than the legacy global isNaN() which performs coercive type casting.

Next Step

Now that you understand data types and memory allocation, let us perform operations and comparisons on these values. Proceed to: JavaScript Operators & Expressions →

Explore Related JavaScript Tutorials

Accelerate your JavaScript mastery by studying these tightly connected core tutorials:

Type Coercion & Casting →

Implicit vs explicit type conversions.

Strings & Template Literals →

String operations and interpolation.

Objects & Properties →

Key-value mapping and reference memory.

Arrays Fundamentals →

Indexed sequential collections in JS.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!