Full answer
JavaScript has seven primitive types: string, number, bigint, boolean, undefined, null, and symbol. Everything else is an object, including arrays, functions, dates, and maps.
A primitive value cannot be changed. A method such as toUpperCase() returns a new string and leaves the original alone. Assigning a primitive to another variable copies the value, and the two variables are independent. Comparing primitives with === compares the values themselves.
An object variable holds a reference, which can be thought of as an address. Assigning it to another variable copies the address, so both names lead to the same object. A change made through one name is visible through the other. Comparing two objects with === asks whether they are the very same object, not whether their contents match.
The same idea applies to function arguments. JavaScript is pass-by-value: the function receives a copy of the variable's value. For a primitive, that is the value. For an object, it is the reference. The function can change the object's contents, but reassigning its own parameter variable does not change the caller's variable.
Frozen objects, const, and copies (spread or structuredClone) are the tools to control accidental sharing.
let a = 10;
let b = a;
b = 20;
console.log(a, b);
const text = 'java';
text.toUpperCase();
console.log(text, text.toUpperCase());
const original = { marks: 80 };
const alias = original;
alias.marks = 95;
console.log(original.marks, original === alias);
console.log({ marks: 95 } === original);
function changeContents(student) {
student.marks = 50;
}
function reassign(student) {
student = { marks: 0 };
return student.marks;
}
changeContents(original);
console.log(original.marks);
console.log(reassign(original), original.marks);
const copy = { ...original };
copy.marks = 1;
console.log(original.marks, copy.marks);
console.log(typeof 42n, typeof Symbol('id'), typeof null, typeof (() => {}));Run with Node.js v24.22.1 as js-24.js; two runs gave identical output. Output: 10 20 java JAVA 95 true false 50 0 50 50 1 bigint symbol object function
Key points
- Seven primitives; everything else is an object.
- Primitives are immutable and copied by value.
- Objects are accessed through references; copies share the same object.
- === on objects compares identity, not contents.
- Arguments are passed by value; for objects the value is a reference.
Common mistakes
- Saying objects are passed by reference. The reference itself is passed by value.
- Assuming that copying with = makes a new object.
- Comparing two arrays or objects with === to test whether contents are the same.
- Thinking a string method changes the string; strings are immutable.
Follow-ups
- What does typeof return for null, arrays, and functions?
- How would you compare two objects by content?
- Where do primitives and objects live in memory, stack or heap?
Tips
- Explain with the two-variable example; it takes ten seconds and is clear.
- Say "pass by value, where the value of an object variable is a reference". This precise phrase impresses interviewers.
- Mention memory only if asked; engines are free to optimize where values are stored.