JavaScript Boxed Primitives
JavaScript temporarily wraps primitive values in corresponding object wrappers when property access or methods require object behavior. This allows values such as strings and numbers to use methods without changing the primitive into a persistent object.
JavaScript has primitive values such as `string`, `number`, `boolean`, `bigint`, `symbol`, `undefined`, and `null`. Primitives are not objects, so they do not themselves contain properties or methods.
When you access a property or method on most primitives, JavaScript performs a temporary boxing operation. For example, calling `"hello".toUpperCase()` can be understood as creating a temporary `String` wrapper, performing the operation, and then discarding that temporary object.
The wrapper constructors include `String`, `Number`, and `Boolean`, with corresponding object types such as `new String("hello")`. Explicitly constructing wrapper objects is generally different from using a primitive: `new String("hello")` produces an object, while `"hello"` remains a primitive.
Autoboxing can make primitive method calls look as though primitives have methods directly. The distinction matters because objects and primitives differ in identity, equality behavior, mutability, and how APIs treat them. `null` and `undefined` are special cases: property access on either throws a `TypeError` rather than boxing them.
Key points
- Primitive method access can trigger temporary boxing.
- `String`, `Number`, and `Boolean` have corresponding wrapper object types.
- `new String("x")` creates an object; `"x"` is still a primitive.
- Boxing is temporary and normally invisible to application code.
- `null` and `undefined` cannot be boxed for ordinary property access.
Interview questions
Why can you call methods on a JavaScript primitive such as a string?
Property and method access on many primitives triggers temporary boxing. JavaScript can use the corresponding wrapper object to resolve the requested property or method. The temporary wrapper does not change the original primitive into an object. This is why `"abc".toUpperCase()` works while `typeof "abc"` remains `"string"`.
What is the difference between `"hello"` and `new String("hello")`?
`"hello"` is a primitive string, whereas `new String("hello")` creates a `String` object. Consequently, `typeof` reports `"string"` for the former and `"object"` for the latter. Strict equality also distinguishes them because one operand is a primitive and the other is an object. In normal application code, primitive strings are generally preferable.
What does autoboxing mean in JavaScript?
Autoboxing is the implicit conversion of a primitive into a temporary wrapper object when an operation needs object-like property access. For example, a string method can be accessed through a temporary `String` wrapper. The wrapper is not normally retained after the operation. This mechanism provides convenient method access while preserving primitive semantics.
Why should frontend code generally avoid wrapper objects such as `new Boolean(false)`?
Wrapper objects introduce object semantics where a primitive is usually expected. In particular, an object is truthy even when it wraps the primitive value `false`, so `if (new Boolean(false))` enters the branch. Wrapper objects also complicate equality and identity checks. Using primitive values avoids these surprising semantics and generally matches API expectations.