Why 'hello'.a Is undefined: JS Objects, Prototypes, and Boxing Explained
Misreading reference vs. value and prototype sharing causes subtle bugs in state management and inheritance; mastering these mechanics lets a developer reason about any object code without guessing. The article turns common interview questions into predictable rules, making it a practical grounding for writing reliable JavaScript.
JavaScript's object semantics come down to three rules: primitives are values, objects are references, and property lookup walks the prototype chain. The wrapper-class trick explains why reading a primitive's property works but writing one silently vanishes. The article walks through real code: 'hello'.length is 5, but 'hello'.a = 2 leaves no trace; two identical-looking object literals fail === equality; and new links an instance to its constructor's prototype in four steps.
Property resolution moves from the object itself up through __proto__ to Object.prototype, and stops at null. Methods belong on the prototype so all instances share one copy; data belongs on the instance so mutations stay isolated. The prototype chain also exposes a classic gotcha: putting an array on the prototype means every instance sees the same array, so pushing through one instance pollutes them all.
Primitives are not objects, but reading a property triggers a temporary boxed String or Number object that is discarded after the access. That's why assigning a property to a primitive is silently lost in sloppy mode and throws in strict mode. Modern tooling includes structuredClone for deep copies, while JSON serialization drops functions, undefined, and symbols, and mangles Date and RegExp.
The wrapper-class mechanism is a hidden cost of JavaScript's convenience: every property read on a primitive allocates and discards a temporary object, and strict mode turns the silent failure into a loud exception.
Shared reference types on prototypes are a footgun: the article's example shows how one instance's push pollutes all siblings, which is why the guidance to keep data on instances is a concrete rule, not style advice.
JavaScript's compatibility quirks (typeof null, referential equality) are deliberate trade-offs that force developers to learn mental models instead of relying on intuition, and the article's systematic walkthrough turns these quirks into predictable rules.