Programming Search⌘K

Featurecore

Primitive wrapper objects

Also called wrapper objects, auto-boxing, boxing, new String, new Number, new Boolean, Object(primitive), ToObject.

String, Number, Boolean, Symbol and BigInt objects wrap a primitive. A property read on a primitive makes a wrapper. new String and new Boolean give objects that act unlike primitives.

Language
JavaScript

Synopsis

Primitive Wrapper new allowed Make a wrapper without new
string String yes Object('a')
number Number yes Object(1)
boolean Boolean yes Object(true)
symbol Symbol no, TypeError Object(Symbol())
bigint BigInt no, error Object(1n)
null, undefined none Object(null) gives {}

"All primitive types, except null and undefined , have their corresponding object wrapper types, which provide useful methods for working with the primitive values."1 The new column is from the Symbol and BigInt pages.2 3

const prim = "foo";
const obj = new String("foo");
console.log(typeof prim, typeof obj, prim.length, obj.length);
console.log(prim === obj, prim == obj, obj.valueOf() === prim);
console.log(new String("a") == new String("a"), new String("a") === new String("a"));
Output
string object 3 3
false true true
false false

Semantics

r1. Auto-boxing. "When properties are accessed on primitives, JavaScript auto-boxes the value into a wrapper object and accesses the property on that object instead." 'foo'.includes('f') "implicitly creates a String wrapper object and calls String.prototype.includes() on that object."4 "This auto-boxing behavior is not observable in JavaScript code".4 Example: synopsis.

r2. ToObject. The spec operation behind a wrapper is ToObject. "If argument is either undefined or null , throw a TypeError exception." For a Boolean, Number or String it returns "a new" wrapper object with the value in an internal slot.5 Example: object-call.

r3. Calls without new give primitives. Strings "returned from String calls in a non-constructor context (that is, called without using the new keyword) are primitive strings."6 String(1), Number('2') and Boolean(0) are conversions, not wrappers. Example: object-call.

r4. new String gives an object. "String with new returns a string wrapper object." Its typeof is 'object'.6 "You should rarely find yourself using String as a constructor."6 So new String('foo') === 'foo' is false. Example: synopsis.

r5. new Boolean(false) is truthy. "Do not use the Boolean() constructor with new". MDN says that "all objects, including a Boolean object whose wrapped value is false , are truthy".7 Example: boolean-trap.

r6. No new for Symbol and BigInt. new Symbol() throws a TypeError. "If you really want to create a Symbol wrapper object, you can use the Object() function".2 BigInt() "Throws an error when called with new ."3 Example: new-symbol.

r7. Back to a primitive. "A String object can always be converted to its primitive counterpart with the valueOf() method."6 Example: synopsis.

Examples

The examples run with Node.js 24.

Object() wraps, the plain calls convert (rules r2 and r3):

console.log(typeof Object(1), typeof Object(1n), typeof Object(Symbol("s")));
console.log(typeof String(1), typeof Number("2"), typeof Boolean(0));
console.log(Object(null), Object(undefined));
Output
object object object
string number boolean
{} {}

Object() called with null or undefined returns an empty object. The ToObject operation itself throws for these values (rule r2).

A Boolean object holding false is truthy (rule r5). MDN gives this case:7

const myFalse = new Boolean(false);
if (myFalse) {
  console.log("printed");
}
console.log(Boolean(myFalse), myFalse.valueOf(), !!false);
Output
printed
true false false

new Symbol() throws (rule r6):

This code raises TypeError, on purpose.

const sym = new Symbol("foo");

String objects and eval (rule r4). "Primitives passed to eval are treated as source code; String objects are treated as all other objects are, by returning the object."6

const s1 = "2 + 2";
const s2 = new String("2 + 2");
console.log(eval(s1), typeof eval(s2), eval(s2.valueOf()));
Output
4 object 4

Pitfalls

new String and new Number in comparisons

Two new String('a') objects are two objects, so === and == both give false for them (synopsis). Use the primitive, or call valueOf() (rules r4 and r7).

new Boolean(false) in a condition

A condition on a Boolean object is always true (boolean-trap). Use Boolean(x) or !!x to convert.7

Properties on a primitive are lost

A property set on a primitive goes to an "ephemeral wrapper object", so it is gone at the next read.4 See Primitive values.

Comparison

Topic JavaScript Python
primitive and object forms both, for string, number, boolean, symbol and bigint1 one form, "All data in a Python program is represented by objects"8
methods on 'abc' on a String wrapper made for the call4 on the str object itself8

Python has no wrapper types, because each value is already an object with "an identity, a type and a value".8 See Objects, values and types.

References

  1. JavaScript data types and data structures, MDN, Primitive values
  2. Symbol, MDN, Description
  3. BigInt, MDN, Description; Constructor
  4. Primitive, MDN Glossary, introduction
  5. ECMAScript 2026 Language Specification, 7.1.18 ToObject
  6. String, MDN, String primitives and String objects
  7. Boolean, MDN, Boolean primitives and Boolean objects; Boolean coercion
  8. Data model, The Python Language Reference, 3.1 Objects, values and types

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source