Boxed Wrappers Differ From Primitives
Primitives get wrapper methods through temporary boxing, but values created with new are objects: different typeof, strict equality fails, Symbol refuses new.
Code
// Primitives are values; wrappers created with new are objects that box them.
const text = 'hello';
const boxed = new String('hello');
console.log('typeof text :', typeof text);
console.log('typeof boxed:', typeof boxed);
console.log('strict equal:', text === boxed);
console.log('loose equal :', text == boxed); // wrapper unwraps via valueOf
// Primitives get wrapper methods through temporary boxing, then let go.
console.log('primitive methods:', text.toUpperCase(), text.length);
// Explicit unwrapping compares the real value underneath the wrapper.
console.log('valueOf equal:', text === boxed.valueOf());
// new Number and new Boolean box the same way.
const n = new Number(7);
console.log('boxed number:', typeof n, n instanceof Number, n.valueOf() === 7);
// Symbol refuses new: there is no wrapper form for it.
try {
new Symbol('x');
} catch (error) {
console.log('new Symbol throws:', error.constructor.name);
}Output
Captured from a sandboxed run of this snippet in the build pipeline.
typeof text : string
typeof boxed: object
strict equal: false
loose equal : true
primitive methods: HELLO 5
valueOf equal: true
boxed number: object true true
new Symbol throws: TypeErrorHow it works
The file’s state is a handful of top-level bindings: text, boxed.
Watch out for
- Ready as-is on a bare install of the language.
Using it
The run emits 8 labeled sections — typeof text :, typeof boxed:, strict equal:, loose equal :, primitive methods:, valueOf equal:, boxed number:, new Symbol throws: — one per step.
Key statements
const boxed = new String('hello');
const n = new Number(7);
const text = 'hello';
Interactive Tools for This Topic
FAQ
- Does this require external packages?
- No. It relies only on built-in language and platform APIs, so you can paste it into a project without installing anything. Its state lives in module-level bindings: `text`, `boxed`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.