JavaScript Number and Math

Number.isNaN versus global isNaN, safe integers, floating-point equality, and Math.trunc versus Math.floor.

モジュールページの本文は現在英語です。

Number and Math are ECMAScript built-ins. They are not Node.js APIs and they are not DOM measurement helpers.

JavaScript has a single Number type, IEEE-754 binary64. Integers above Number.MAX_SAFE_INTEGER are not all distinct. 0.1 + 0.2 is not exactly 0.3. Global isNaN and isFinite coerce their argument first; Number.isNaN and Number.isFinite do not.

Examples were checked in Node.js 22, which implements these ECMAScript built-ins. See the MDN documentation for the complete API.

JavaScript tutorial and API overview →

API index

APISummary
Number.isNaN, isNaN, Number.isFinite, Number, parseIntTest for NaN or finiteness without coercion, and convert text with Number or parseInt. The global isNaN and isFinite functions coerce first.
Number.isSafeInteger, Number.MAX_SAFE_INTEGER, Number.EPSILON, Math.trunc, Math.floor, Math.round, Math.hypotTell when an integer is in the safe range, inspect floating-point rounding, and choose trunc or floor for negative numbers.

isNaN, Number.isNaN, and conversion

Number.isNaN(value) is true only when the value is the number NaN. The global isNaN(value) first converts the argument with ToNumber, so isNaN("foo") and isNaN(undefined) are true, while Number.isNaN("foo") and Number.isNaN(undefined) are false. The same split exists between isFinite and Number.isFinite: isFinite("1") is true, Number.isFinite("1") is false.

Number("") and Number(" ") are 0, which is why isNaN("") is false. Number("8px") is NaN. parseInt("10px", 10) is 10 and stops at the first invalid digit. Always pass a radix to parseInt. parseInt("0x10") is 16, but parseInt("0x10", 10) is 0 because the "x" ends a base-10 parse after the leading 0. NaN === NaN is false; Object.is(NaN, NaN) is true.

Number.isNaN, isNaN, Number.isFinite, Number, parseInt

Number.isNaN(value)
isNaN(value)
Number.isFinite(value)
Number(value)
parseInt(string, radix)

Test for NaN or finiteness without coercion, and convert text with Number or parseInt. The global isNaN and isFinite functions coerce first.

ParameterDescription
valueFor Number.isNaN and Number.isFinite, the value is not coerced. For the global functions, it is converted with ToNumber first.
string, radixparseInt parses a prefix of the string in the given radix. The radix should be supplied explicitly, usually 10.

Returns: The isNaN and isFinite functions return a boolean. Number returns a number, which may be NaN. parseInt returns an integer or NaN.

  • isNaN("") is false because Number("") is 0. That is a common source of empty-input bugs.
javascript
console.log(isNaN(NaN), isNaN("foo"), isNaN(undefined), isNaN(""), isNaN(" "));
console.log(Number.isNaN(NaN), Number.isNaN("foo"), Number.isNaN(undefined), Number.isNaN(""), Number.isNaN(" "));
console.log(isFinite("1"), Number.isFinite("1"), Number.isFinite(1), Number.isFinite(Infinity));
console.log(Number(""), Number(" "), Number("  8  "), Number("8px"));
console.log(parseInt("10px", 10), parseInt("08", 10), parseInt("0x10"), parseInt("0x10", 10));
console.log(NaN === NaN, Object.is(NaN, NaN));

Expected output

true true true false false
true false false false false
true false true false
0 0 8 NaN
10 8 16 0
false true

MDN documentation →

Safe integers, rounding, and Math

Number.MAX_SAFE_INTEGER is 9007199254740991, or 2^53 - 1. Number.isSafeInteger is true only for a finite integer in that inclusive range. MAX_SAFE_INTEGER + 1 and MAX_SAFE_INTEGER + 2 are the same Number value, so they compare as equal. Number.EPSILON is the difference between 1 and the next representable number after 1. It is not a universal "close enough" tolerance: 0.1 + 0.2 === 0.3 is false, and the residual 0.1 + 0.2 - 0.3 is smaller than EPSILON but still not zero.

Math.trunc drops the fractional part toward zero. Math.floor returns the next integer toward -Infinity, so Math.trunc(-1.8) is -1 and Math.floor(-1.8) is -2. Math.round goes to the nearest integer and, on a tie (a fractional part of exactly 0.5), rounds toward +Infinity, so Math.round(-1.5) is -1. Math.hypot(3, 4) is 5.

Number.isSafeInteger, Number.MAX_SAFE_INTEGER, Number.EPSILON, Math.trunc, Math.floor, Math.round, Math.hypot

Number.isSafeInteger(value)
Number.MAX_SAFE_INTEGER
Number.EPSILON
Math.trunc(x)
Math.floor(x)
Math.round(x)
Math.hypot(...values)

Tell when an integer is in the safe range, inspect floating-point rounding, and choose trunc or floor for negative numbers.

ParameterDescription
value / xA Number. isSafeInteger is false for non-integers, NaN, and integers outside the safe range.

Returns: isSafeInteger returns a boolean. The Math methods return numbers. hypot returns the square root of the sum of squares.

  • These numeric results were measured in Node.js 22 and follow the IEEE-754 values of the language, not a host timezone or locale.
javascript
console.log(Number.MAX_SAFE_INTEGER);
console.log(
  Number.isSafeInteger(Number.MAX_SAFE_INTEGER),
  Number.isSafeInteger(Number.MAX_SAFE_INTEGER + 1),
);
console.log(Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2);
console.log(0.1 + 0.2 === 0.3, 0.1 + 0.2 - 0.3, Number.EPSILON);
console.log(Math.trunc(-1.8), Math.floor(-1.8), Math.round(-1.5), Math.hypot(3, 4));

Expected output

9007199254740991
true false
true
false 5.551115123125783e-17 2.220446049250313e-16
-1 -2 -1 5

MDN documentation →