Course contentsShow
← JavaScript
Lesson 44 of 2,7232. Values and TypesFree lesson

When Not to Use new String(), new Number()

Learn why explicit wrapper constructors create objects and should be avoided in modern code.

When Not to Use new String(), new Number()

What you'll learn: Why using new with wrapper constructors creates confusing objects instead of simple primitives.

The Problem with new

In the previous lesson, you learned that JavaScript automatically wraps primitives in temporary objects when needed. However, you can explicitly create wrapper objects using new String(), new Number(), or new Boolean(). This is almost always a mistake.

When you use new, you don't get a primitive—you get an actual object:

let primitive = "hello";
let object = new String("hello");

typeof primitive;  // "string"
typeof object;     // "object" ⚠️

Why This Causes Problems

1. Type confusion: The object version behaves differently than the primitive:

let a = "hello";
let b = new String("hello");

a === "hello";  // true
b === "hello";  // false (object !== primitive)

2. Truthiness issues: Even an "empty" wrapper object is truthy:

if (new Boolean(false)) {
  console.log("This runs!"); // Executes, even though value is false
}

The Simple Rule

Never use new with primitive wrappers. JavaScript's automatic wrapping handles everything you need. If you want a primitive, just create it directly:

let str = "hello";     // ✓ Good
let num = 42;          // ✓ Good

let str2 = new String("hello");  // ✗ Avoid
let num2 = new Number(42);       // ✗ Avoid

Key Takeaway: Using new String(), new Number(), or new Boolean() creates objects instead of primitives, causing unexpected behavior—always create primitives directly without new.