Skip to content
  • Primitives vs References · JavaScript

Primitives vs References

LearnViaNotes @learnvianotes· 2 min read

Primitives are copied by value; objects are shared by reference. Almost every “why did my data change?” bug comes from this.

Copying a primitive

JavaScript
let a = 10;
let b = a;     // b gets its own copy of the value
b = 20;
console.log(a, b);
Output
10 20

Copying an object reference

JavaScript
const original = { score: 10 };
const alias = original;   // copies the *reference*, not the object
alias.score = 99;
console.log(original.score);
console.log(original === alias);
Output
99
true
PrimitivesObjects (incl. arrays, functions)
Stored asThe value itselfA reference to a heap object
Assignment copiesThe valueThe reference
=== comparesValuesIdentity (same object?)
Mutable?No — immutableYes

Equality compares identity for objects

JavaScript
console.log("abc" === "abc");
console.log({ a: 1 } === { a: 1 });
console.log([1, 2] === [1, 2]);
const arr = [1, 2];
console.log(arr === arr);
Output
true
false
false
true

Passing to functions: always by value (of the reference)

JavaScript is pass-by-value. For objects the value being passed is a reference — so a function can mutate the caller's object, but reassigning the parameter never affects the caller.

JavaScript
function mutate(obj) {
  obj.touched = true;   // changes the shared object
}
function reassign(obj) {
  obj = { replaced: true };  // only rebinds the local parameter
}
const data = {};
mutate(data);
reassign(data);
console.log(data);
Output
{ touched: true }

Primitives are immutable

JavaScript
const s = "hello";
const upper = s.toUpperCase();   // returns a NEW string
console.log(s, upper);
const t = "cat";
try {
  t[0] = "b";                    // strings can't change in place
} catch (e) {
  console.log(e.name + ": " + e.message);
}
console.log(t);
Output
hello HELLO
TypeError: Cannot assign to read only property '0' of string 'cat'
cat

In strict mode (every ES module) the assignment throws; in a sloppy script it is silently ignored. Either way the string never changes.

Is JavaScript pass-by-reference?

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report