Objects vs Primitives
Master this topic with zero to advance depth.
Expert Answer & Key Takeaways
Mastering Objects vs Primitives is essential for high-fidelity technical architecture and senior engineering roles in 2026.
JavaScript Object References
Objects are reference types. Unlike primitives (strings, numbers, booleans) which are passed by value, objects are addressed by a reference to their location in memory.
1. Equality vs Identity
Two objects are never equal, even if they have the same properties.
const x = {name: "John"};
const y = {name: "John"};
console.log(x == y); // false
console.log(x === y); // falseTwo objects are only equal if they refer to the exact same memory address:
const x = {name: "John"};
const y = x;
console.log(x === y); // true (y is a reference to x)2. Shallow Copy vs Deep Copy
When you assign an object to a new variable, you are creating a new reference, not a new object. Changing
y will also change x.
Shallow Copy (Object.assign or Spread):
Creates a new object, but nested objects are still references.const original = { a: 1, b: { c: 2 } };
const copy = { ...original };
copy.a = 99;
console.log(original.a); // 1 (preserved)
copy.b.c = 99;
console.log(original.b.c); // 99 (nested object was shared!)Deep Copy (structuredClone):
Modern 2026 syntax
structuredClone() creates a completely independent copy, including all nested levels.const original = { a: 1, b: { c: 2 } };
const deepCopy = structuredClone(original);
deepCopy.b.c = 99;
console.log(original.b.c); // 2 (preserved!)[!IMPORTANT]structuredClone()is the modern standard for deep cloning. Avoid usingJSON.parse(JSON.stringify(obj))as it loses date objects and functions.
Top Interview Questions
?Interview Question
Q:Why does {a:1} === {a:1} return false?
A:
In JavaScript, objects are compared by reference, not by value. Since these are two distinct objects created in different memory locations, the comparison returns false even if their contents are identical.
?Interview Question
Q:How do you create a deep copy of an object in modern JavaScript?
A:
The built-in 'structuredClone()' function is the modern, recommended way to create a deep copy of an object, preserving nested structures.
?Interview Question
Q:What is a major risk of shallow copying?
A:
A shallow copy only copies the top-level properties. If the object contains nested objects or arrays, those remain shared references. Changing a nested value in the copy will unintentionally change it in the original object.
Course4All Engineering Team
Verified ExpertSenior Full-Stack Engineers & V8 Experts
Our JavaScript and engine-level content is developed by a collective of senior engineers focused on high-performance web architecture and 2026 standards.
Pattern: 2026 Ready
Updated: Weekly
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.