InterviewHubPRO
Section Overview
Q2

Question 2 of 521

Part 1: Core Objects, Prototypes, Functions & Scope (1 - 53)

0%
JavaScript Internals (521 Qs)•Part 1: Core Objects, Prototypes, Functions & Scope (1 - 53)

What are the primitive and reference data types in JavaScript, and how are they stored in memory?

In Simple Words: JavaScript divides data types into two primary categories: Primitives (immutable values stored directly by value in Call Stack memory) and Reference Types (mutable objects stored in Heap memory, with stack pointers referencing their heap addresses).

The 7 Primitive Types:

  1. string: Immutable sequence of UTF-16 characters ("text", 'text', text).
  2. number: IEEE 754 64-bit double-precision floating-point format (e.g. 42, 3.14, NaN, Infinity).
  3. bigint: Arbitrary-precision integer for numbers exceeding Number.MAX_SAFE_INTEGER (e.g. 9007199254740991n).
  4. boolean: Logical entity representing true or false.
  5. undefined: Automatically assigned default value for declared variables that have not been assigned a value.
  6. symbol: Unique, immutable identifier typically used as object property keys (Symbol('id')).
  7. null: Intentional absence of any object value. (Note: typeof null === 'object' is a historical bug in JavaScript's initial implementation).

Reference Types (Objects): All non-primitive structures inherit from Object.prototype:

  • Plain Objects ({ a: 1 }), Arrays ([1, 2, 3]), Functions (function() {}), Dates (new Date()), Regular Expressions (/abc/), Map, Set, WeakMap, WeakSet.

Practical Code Example

javascript
// 1. Primitive: Pass-by-value (Stack allocation)
let a = 10;
let b = a; // Copied by value
b = 20;

console.log(a); // 10 (unaffected)
console.log(b); // 20

// 2. Reference Type: Pass-by-reference (Heap allocation & pointer copy)
const user1 = { name: "Alex Morgan", role: "Staff Engineer" };
const user2 = user1; // Copies pointer address, both point to same heap object

user2.role = "Principal Architect";

console.log(user1.role); // "Principal Architect" (mutated via shared reference)
console.log(user1 === user2); // true (same reference pointer)

// 3. Type Checking & typeof null historical quirk
console.log(typeof "hello");        // "string"
console.log(typeof 42);             // "number"
console.log(typeof 100n);           // "bigint"
console.log(typeof true);           // "boolean"
console.log(typeof undefined);      // "undefined"
console.log(typeof Symbol("key"));  // "symbol"
console.log(typeof null);           // "object" (historical JavaScript quirk!)
console.log(typeof {});             // "object"
console.log(typeof []);             // "object"
console.log(typeof function(){});   // "function"

// Safe check for null:
const isNull = (val) => val === null;
console.log(isNull(null)); // true