Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Number — converting values to numbers

Number(value) converts a value to a primitive number. Learn the difference between conversion and a new Number(value) wrapper object, and how Number.prototype.toString converts a number to text.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

Number(value)
Number()
new Number(value)
number.toString(radix)

Number(value) returns a primitive number; Number() returns 0. In contrast, new Number(value) creates a wrapper object: its typeof is "object", not "number". For ordinary calculations and conversions, use the call without new.

String conversion considers the entire representation after trimming surrounding whitespace: Number("255px") produces NaN, whereas Number("") produces 0. Number(null) produces 0, Number(undefined) produces NaN, and booleans become 1 or 0. Converting a Symbol throws TypeError. Number can explicitly convert a BigInt, but the result may lose precision.

The number type uses IEEE 754 double-precision binary format: many decimal fractions are approximate. The safe integer range is -(2 ** 53 - 1) through 2 ** 53 - 1; beyond it, not every integer is represented exactly.

Number.prototype.toString works in the opposite direction: value.toString(radix) returns text. The default radix is 10; use an integer radix from 2 through 36, such as 16 for hexadecimal. An out-of-range radix after conversion throws RangeError. The method requires a number or a Number object as this; another receiver causes TypeError.

Examples

const value = Number(" 255 ");
console.log(typeof value);
console.log(value);
console.log(value.toString(16));
console.log(Number(""));
console.log(Number.isNaN(Number("255px")));

Result

number
255
ff
0
true

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources