Skip to content

number

In JavaScript, number is a primitive type that represents both integers and fractional values as 64-bit IEEE 754 double-precision floating-point numbers. It’s one of JavaScript’s primitive types, distinct from the Number wrapper object, whose prototype supplies its methods:

Language: JavaScript
> 42
42

> 3.14
3.14

> typeof 42
'number'

Numbers are immutable and compare by value. Strict equality with === treats NaN as unequal to itself and 0 as equal to -0, while Object.is() reverses both results.

number Literals and typeof

A number literal can be written in decimal, exponent, hexadecimal, octal, or binary form, and underscores work as numeric separators. The special values NaN and Infinity are properties of the global object, not literals, but typeof still reports them as 'number':

Language: JavaScript
> 1_000_000
1000000

> 2.5e3
2500

> [0xff, 0o17, 0b101]
[ 255, 15, 5 ]

> typeof NaN
'number'
> typeof Infinity
'number'

Whole numbers and fractions share the same type, so typeof 1 and typeof 1.5 both return 'number'. Integers beyond the safe range need the bigint type instead.

Converting to number

Number(value) converts explicitly, and the unary + operator applies the same conversion, except that it throws a TypeError on a bigint. Some results follow from type coercion rules that surprise many readers:

Language: JavaScript
> Number("")
0

> Number(" 42 ")
42

> Number("42px")
NaN

> Number(null)
0

> Number(undefined)
NaN

> +true
1

> Number(10n)
10

Common number Gotchas

Decimal fractions such as 0.1 have no exact binary representation, so arithmetic on them can be slightly off:

Language: JavaScript
> 0.1 + 0.2
0.30000000000000004

> 0.1 + 0.2 === 0.3
false

NaN is the only value that isn’t equal to itself, so a reliable check uses Number.isNaN() instead of comparing with ===:

Language: JavaScript
> NaN === NaN
false

> Number.isNaN(NaN)
true

Above Number.MAX_SAFE_INTEGER, not every integer can be represented, so adjacent integers can become indistinguishable:

Language: JavaScript
> Number.MAX_SAFE_INTEGER
9007199254740991

> 2 ** 53 === 2 ** 53 + 1
true

Negative zero is a separate value that === can’t tell apart from 0:

Language: JavaScript
> -0 === 0
true

> Object.is(-0, 0)
false

> 1 / -0
-Infinity

Mixing number and bigint in arithmetic throws a TypeError, so one operand needs an explicit conversion first.

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 4, 2026