Data Types in JavaScript: Primitive vs Reference

Easy100% Free~15 mins#primitives#references#memory#v8#mutation#pass-by-value
Interview Answer (30-Second Summary)

Data types in JavaScript split into primitives and reference types: primitives are immutable values stored directly by value, whereas reference types (objects, arrays, functions) are mutable entities stored on the heap and accessed via memory references. Variable assignment and function arguments in JavaScript are always passed by value; for objects, the value passed is the memory reference itself.

Key Learning Objectives
✓

Distinguish between JavaScript primitive types and reference types in V8 engine memory.

✓

Understand stack allocation for primitive values vs heap allocation for objects.

✓

Predict mutation side effects when multiple variables reference the same heap object.

✓

Recognize why reassigning an object reference parameter does not mutate the caller argument.

The Interview Problem

What is printed to the console when the following JavaScript code executes, and why does mutating a property behave differently than reassigning the parameter?

1let primitiveVal = 42;
2let objA = { count: 1 };
3let objB = objA;
4
5function update(num, target) {
6 num = 100;
7 target.count = 5;
8 target = { count: 999 };
9}
10
11update(primitiveVal, objA);
12
13console.log(primitiveVal, objA.count, objB.count);
Predict Console Output
Interactive Challenge

Select the option that matches what this code prints to the console:

42 5 5

100 5 5

42 999 999

42 5 1

V8 Engine Execution Trace
Step 1 of 8 (Line 1)

Allocate primitive value 42 in the global execution context stack frame.

Call Stack (Top = Active)
Global Execution Context
Lexical Scope / Bindings
primitiveVal:42
Console Stream
> [empty]

Data Types in JavaScript Explained

Core Mental Model: Pass by Value vs Pass by Reference Value

JavaScript has 7 primitive types (string, number, bigint, boolean, undefined, symbol, null) and 1 composite reference type (Object, which includes Functions and Arrays).

  1. Primitives are Immutable and Stored by Value: When a primitive is assigned to another variable or passed to a function, a distinct copy of the bit-level value is pushed onto the stack. Changing one variable never influences another.
  2. Objects are Stored in the Heap and Accessed by Pointer: When objB = objA runs, no new object is created in memory. Instead, both variables contain a pointer to the same heap location. Mutating target.count = 5 traverses the pointer and mutates the heap memory directly.
  3. Reassignment Breaks the Link: When target = { count: 999 } executes, it overwrites the local pointer in target with the memory address of a freshly allocated object. It does not overwrite the memory address stored in objA or objB.
Common Traps & Mistakes

Assuming `const` prevents object mutation. `const` only prevents variable reassignment; properties can still be modified.

Confusing parameter reassignment with mutation. Reassigning a function parameter has zero effect on the caller's reference.

Using shallow cloning (`{ ...obj }` or `Object.assign`) for deeply nested structures where nested objects are still shared references.

FAANG Follow-Up Probes
Probe #1

How does `structuredClone()` differ from `JSON.parse(JSON.stringify(obj))` when copying complex data structures?

Probe #2

Why does `Object.freeze()` only create a shallow freeze, and how would you implement a recursive `deepFreeze` utility?

Probe #3

How does V8 optimize small integers (Smi) and heap numbers under the hood?

Data Types in JavaScript Interview Questions

Key interview inquiries, edge cases, and architectural follow-ups for Primitive vs Reference Types.

Q1.
Is JavaScript pass-by-reference or pass-by-value?

JavaScript is strictly pass-by-value in all situations. For primitive types, the actual bit-level value is copied into the parameter. For reference types, the memory pointer pointing to the heap object is copied by value, meaning mutations to properties affect the shared object, but reassigning the parameter variable has no effect on the caller.

A shallow copy duplicates only the top-level properties of an object; any nested objects or arrays retain their original memory references and remain shared. A deep copy recursively duplicates every nested object, ensuring complete memory isolation between the original and cloned data structures.

Primitive values cannot be modified in place. Operations on primitives, such as string concatenation or calling .toUpperCase(), do not alter the underlying memory; instead, they compute and return a brand new primitive value while leaving the original untouched.

In V8, small integers (Smis) and primitive values held in local execution contexts are allocated on the call stack or encoded directly in pointer tags. Objects, arrays, closures, and boxed primitives reside on the V8 managed heap and are reclaimed by the garbage collector when no retaining paths exist.

When an object is passed to a function, the local parameter receives a copy of the pointer to the heap object. Mutating a property traverses that pointer to mutate the heap memory directly. Reassigning the parameter rebinds the local parameter to a different memory address without overwriting the caller variable.

When accessing a method or property on a primitive string, number, or boolean (e.g., "abc".length), JavaScript temporarily creates an implicit object wrapper (String, Number, Boolean) so the prototype method can execute, then immediately discards the wrapper object.

const prevents reassigning the variable identifier to a new memory address, but does not prevent property mutation on objects. Object.freeze() makes an object shallowly immutable by marking all existing properties as writable: false and configurable: false, preventing additions, deletions, and modifications.

More JavaScript interview questions for freshers