Primitive vs Object
Primitives are immutable single values copied by value and compared by content. Objects are mutable containers held by reference, copied as references, and compared by identity. Nearly every "why did my data change?" bug comes from that difference.
Detailed Explanation
The seven primitive types are string, number, boolean, null, undefined, symbol, and bigint. Everything else — plain objects, arrays, functions, Map, Date, class instances — is an object.
Primitives are immutable. Not "hard to change", genuinely unchangeable. name.toUpperCase() does not modify name; it returns a new string. str[0] = "X" does nothing at all in sloppy mode and throws in strict mode. When you write let a = 5; let b = a;, the number is copied, and the two bindings are independent from then on.
Objects work the other way. A variable holding an object holds a reference to it. const b = a copies that reference, so both names reach the same object, and a mutation through either name is visible through the other. This is why a function that receives a configuration object and sets a property on it has changed the caller's object. JavaScript passes arguments by value, but for objects the value being passed is a reference, which produces the shared-mutation behaviour people describe loosely as pass-by-reference.
Equality follows the same split. "abc" === "abc" is true because the contents match. {} === {} is false because they are two separate objects, no matter how identical they look. Comparing objects meaningfully requires comparing their contents yourself, or via a library.
Then why can you call methods on a primitive? Because of autoboxing. When you write "ana".toUpperCase(), the engine temporarily wraps the string in a String wrapper object, looks up the method on String.prototype, calls it, and throws the wrapper away. The consequence is that assigning a property to a primitive appears to succeed and then vanishes: the property lands on the temporary wrapper, which is discarded immediately. null and undefined have no wrapper, which is why property access on them throws TypeError: Cannot read properties of null.
You should never construct wrappers yourself. new String("ana") produces an object, so typeof reports "object", === "ana" is false, and the value is truthy even when it wraps an empty string. Use String(x), Number(x), and Boolean(x) as plain functions for conversion.
Copying an object requires an explicit decision about depth. { ...original } and Object.assign({}, original) give a shallow copy: top-level primitives are duplicated, but nested objects are still shared. For a genuinely independent copy, structuredClone(original) is the built-in answer, available in modern browsers and Node 17 and later. It handles Map, Set, Date, and cycles, but it cannot clone functions or DOM nodes.
There is a cost story here too. Passing a large object around is free, because only a reference moves; copying it deeply is not. Meanwhile engines can keep small primitives inline and stack-allocated while objects live on the heap and need garbage collection. That is an implementation detail rather than a spec rule, but it is why a million numbers in a typed array is far cheaper than a million small wrapper objects.
Code Examples
Copy semantics: value versus reference.
// Copy semantics: value versus reference.
let priceA = 100;
let priceB = priceA; // the number is copied
priceB = 150;
console.log(priceA, priceB); // => 100 150
const cartA = { items: 2 };
const cartB = cartA; // the reference is copied
cartB.items = 5;
console.log(cartA.items); // => 5 one object, two names
console.log({ items: 5 } === { items: 5 }); // => false objects compare by identityAutoboxing: methods work, but new properties do not stick.
// Autoboxing: methods work, but new properties do not stick.
const user = "ana";
console.log(user.toUpperCase()); // => ANA a temporary String wrapper provided the method
try {
user.role = "admin"; // the property lands on a throwaway wrapper
} catch (err) {
console.log(err.name); // => TypeError, in strict mode and in modules
}
console.log(user.role); // => undefined nothing was stored either way
// Never build wrappers by hand.
const wrapped = new String("ana");
console.log(typeof wrapped); // => object
console.log(wrapped === "ana"); // => false
console.log(Boolean(new Boolean(false))); // => true an object is always truthyShallow copy shares nested objects; structuredClone does not.
// Shallow copy shares nested objects; structuredClone does not.
const original = { plan: "pro", limits: { seats: 5 } };
const shallow = { ...original };
shallow.limits.seats = 50;
console.log(original.limits.seats); // => 50 nested object was shared
const deep = structuredClone(original); // Node 17+ and modern browsers
deep.limits.seats = 500;
console.log(original.limits.seats); // => 50 original untouched this timeCommon Gotchas
A shallow copy is not a copy of your nested data
spread and `Object.assign` duplicate only the top level. Mutating `copy.settings.theme` still hits the original. Use `structuredClone` when the value has depth.
Property access on `null` or `undefined` throws
these two primitives have no wrapper object, so `user.name` on `null` raises `TypeError: Cannot read properties of null`. Guard with optional chaining.
`const` does not make an object primitive-like
`const` fixes the binding; the object stays mutable. `Object.freeze` blocks top-level writes, and even that is shallow.
Equality mirrors the split
primitives compare by content, objects by identity. Two arrays with the same elements are never `===`, which is why deep-equality helpers exist in every test framework.