New Wrapper Constructor

ID

javascript.new_wrapper_constructor

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

JavaScript

Tags

best-practice, reliability

Description

Reports new String(…​), new Number(…​), and new Boolean(…​). The String, Number, and Boolean functions called with new produce object wrappers, not primitives. The wrappers behave subtly differently from the primitives they envelop — typeof new Boolean(false) === "object", if (new Boolean(false)) is truthy, Object.is(new Number(1), 1) is false.

Rationale

  • new Boolean(false) is the most common gotcha — every wrapper, including the one wrapping false, is truthy in a boolean context.

  • Object.is and === distinguish wrapper objects from primitives, breaking equality logic.

  • Wrappers serialise differently and confuse JSON / structuredClone consumers.

// Bad
const flag = new Boolean(false);    // truthy
const num = new Number("42");        // typeof === "object"

Remediation

Use the plain conversion functions without new to coerce values into primitives.

// Good
const flag = Boolean(false);
const num = Number("42");
const str = String(value);