J

JavaScript Handbook

Clean • Professional

Number Methods

2 minute

Number Methods

JavaScript offers both basic and static number methods to perform operations on, manipulate, and analyze numeric values.

1. Basic Number Methods

These methods can be called on any number or Number object.

toString()

Converts a number to a string, optionally specifying a radix (base).

let num = 42;
console.log(num.toString());   // "42"
console.log(num.toString(2));  // "101010" (binary)
console.log(num.toString(16)); // "2a" (hexadecimal)

toExponential()

Returns a string representing the number in exponential notation with a specified number of digits after the decimal point.

let num = 123.456;
console.log(num.toExponential());   // "1.23456e+2"
console.log(num.toExponential(2)); // "1.23e+2"

toFixed()

Formats a number with a fixed number of digits after the decimal point.

let num = 3.14159;
console.log(num.toFixed(2)); // "3.14"
console.log(num.toFixed(0)); // "3"

toPrecision()

Formats a number to a specified total number of significant digits.

let num = 123.456;
console.log(num.toPrecision(4)); // "123.5"
console.log(num.toPrecision(2)); // "1.2e+2"

valueOf()

Returns the primitive numeric value of a Number object.

let num = new Number(42);
console.log(num.valueOf());            // 42
console.log(typeof num);               // "object"
console.log(typeof num.valueOf());     // "number"

2. Static Number Methods

These methods are called on the Number constructor, not individual numbers.

Number.isFinite()

Checks if a value is a finite number (not Infinity, -Infinity, or NaN).

console.log(Number.isFinite(42));       // true
console.log(Number.isFinite(Infinity)); // false
console.log(Number.isFinite(NaN));      // false
console.log(Number.isFinite("42"));     // false

Number.isInteger()

Checks if a value is an integer.

console.log(Number.isInteger(42));    // true
console.log(Number.isInteger(42.0));  // true
console.log(Number.isInteger(42.5));  // false
console.log(Number.isInteger("42"));  // false

Number.isNaN()

Checks if a value is NaN. Unlike the global isNaN(), it does not convert non-numbers.

console.log(Number.isNaN(NaN));         // true
console.log(Number.isNaN(42));          // false
console.log(Number.isNaN("NaN"));       // false
console.log(Number.isNaN(undefined));   // false

Number.isSafeInteger()

Checks if a value is an integer within the safe range (Number.MIN_SAFE_INTEGER to Number.MAX_SAFE_INTEGER).

console.log(Number.isSafeInteger(42));                     // true
console.log(Number.isSafeInteger(9007199254740992));      // false
console.log(Number.isSafeInteger(42.5));                  // false
console.log(Number.isSafeInteger("42"));                  // false

Number.parseInt()

Parses a string and returns an integer, optionally specifying a radix.

console.log(Number.parseInt("42"));       // 42
console.log(Number.parseInt("42.5"));     // 42
console.log(Number.parseInt("1010", 2));  // 10 (binary)
console.log(Number.parseInt("abc"));      // NaN

Number.parseFloat()

Parses a string and returns a floating-point number.

console.log(Number.parseFloat("3.14"));     // 3.14
console.log(Number.parseFloat("42"));       // 42
console.log(Number.parseFloat("3.14abc"));  // 3.14
console.log(Number.parseFloat("abc"));      // NaN

Basic Methods

These methods can be called directly on a number:

MethodDescriptionExample
toString()Converts a number to a string(123).toString(); // "123"
toExponential(n)Returns a string in exponential notation with n decimals(1234).toExponential(2); // "1.23e+3"
toFixed(n)Formats a number with n decimal places(3.14159).toFixed(2); // "3.14"
toPrecision(n)Formats a number to n significant digits(3.14159).toPrecision(3); // "3.14"
valueOf()Returns the primitive value of a number(123).valueOf(); // 123

Static Methods

These methods are called on the Number object, not on individual numbers:

MethodDescriptionExample
Number.isFinite(value)Checks if a value is a finite numberNumber.isFinite(10); // true
Number.isInteger(value)Checks if a value is an integerNumber.isInteger(10.5); // false
Number.isNaN(value)Checks if a value is NaNNumber.isNaN(NaN); // true
Number.isSafeInteger(value)Checks if a value is a safe integerNumber.isSafeInteger(9007199254740991); // true
Number.parseInt(string)Converts a string to an integerNumber.parseInt("123abc"); // 123
Number.parseFloat(string)Converts a string to a floating-point numberNumber.parseFloat("3.14abc"); // 3.14


Article 0 of 0