How JavaScript Boxes Primitive Values: Prototypes, __proto__, and the Wrapper Class Illusion
Auto-boxing and the prototype chain are the root of two easy-to-miss bugs: silently lost property assignments on primitives and unexpected method lookup behavior. Once you know exactly when V8 creates and destroys wrapper objects, you can predict these outcomes instead of being surprised by them.
JavaScript's object system separates primitive values (number, string, boolean, bigint, symbol) from reference types (object, array, function). Primitives are not objects at definition time; V8 defers object creation until a property or method is accessed, at which point it runs the corresponding wrapper constructor and produces a disposable temporary object. That mechanism, called auto-boxing, lets you call 'str'.toUpperCase(), but it also means any property you write to a primitive lands on a temporary object that is destroyed before you can read it back, so s.age = 18 followed by console.log(s.age) yields undefined.
Constructors, when invoked with new, produce instances whose __proto__ points to the constructor's prototype. Property lookup checks own properties first, then climbs __proto__ up the prototype chain, which is why methods defined on Person.prototype are shared by all Person instances. The article walks through the exact execution sequence, clarifies that only functions have prototype and only objects have __proto__, and notes that null and undefined have no wrapper classes, so calling methods on them throws. The common phrase 'everything is an object' holds only through the temporary boxing illusion; reference types are objects by nature, while primitives merely borrow object behavior for the duration of a single property access.
The 'everything is an object' motto is a convenience that only works because of auto-boxing; the illusion collapses the moment you try to persist a property on a primitive.
The temporary wrapper's lifecycle explains both why method calls work and why property writes fail: they go through the same object, but the object is gone before you can observe the write.
The distinction between prototype and __proto__ is a frequent source of confusion because one is a function property and the other is an object property, but the equality p1.__proto__ === Person.prototype ties them together.