JavaScript Number Methods

JavaScript Number Methods

Thesenumeric methodsCan be used with all JavaScript numbers:

Method Description
toString() Returns a number as a string.
toExponential() Returns a number written in exponential notation.
toFixed() Returns a number with decimal places.
toPrecision() Returns a number of specified length.
ValueOf() Returns a number in numeric form.

toString() Method

The toString() method returns a number as a string.

All number methods can be used with any type of number (text, variable, or expression):

Example

let x = 123;
x.toString();
(123).toString();
(100 + 23).toString();

Try it yourself

toExponential() Method

The toExponential() method returns a string with a number rounded and written in exponential notation.

The parameter defines the number of characters after the decimal point:

Example

let x = 9.656;
x.toExponential(2);
x.toExponential(4);
x.toExponential(6);

Try it yourself

This parameter is optional. If you do not specify it, JavaScript will not round the number.

toFixed() Method

The toFixed() method returns a string with a number having a decimal part with a specified number of digits.

Example

let x = 9.656;
x.toFixed(0);
x.toFixed(2);
x.toFixed(4);
x.toFixed(6);

Try it yourself

Tip:toFixed(2) is very suitable for handling currency.

toPrecision() method

The toPrecision() method returns a string containing a number of specified length:

Example

let x = 9.656;
x.toPrecision();
x.toPrecision(2);
x.toPrecision(4);
x.toPrecision(6);

Try it yourself

valueOf() method

valueOf() returns the number in numeric form.

Example

let x = 123;
x.valueOf();
(123).valueOf();
(100 + 23).valueOf();

Try it yourself

In JavaScript, numbers can be primitive values (typeof = number) or objects (typeof = object).

The valueOf() method is used internally in JavaScript to convert Number objects to primitive values.

There is no reason to use it in your code.

Tip:All JavaScript data types have valueOf() and toString() methods.

Convert variables to numbers

There are 3 JavaScript methods that can be used to convert variables to numbers:

Method Description
Number() return the number converted from its parameters.
parseFloat() parse its parameters and return a floating-point number.
parseInt() parse its parameters and return an integer.

The methods above are notnumeric methods. They areGlobal JavaScript method.

Number() method

The Number() method can be used to convert JavaScript variables to numbers:

Example

Number(true);
Number(false);
Number("10");
Number("  10");
Number("10  ");
Number(" 10  ");
Number("10.33");
Number("10,33");
Number("10 33");
Number("Bill");

Try it yourself

Tip:If the number cannot be converted, it returns NaN (Not a Number, non-numeric).

The Number() method used on dates

Number() can also convert dates to numbers.

Example

Number(new Date("1970-01-01"))

Try it yourself

Note:The Date() method returns the number of milliseconds since January 1, 1970.

The number of milliseconds between 1970-01-02 and 1970-01-01 is 86400000:

Example

Number(new Date("1970-01-02"))

Try it yourself

Example

Number(new Date("2017-09-30"))

Try it yourself

parseInt() method

parseInt() parses a string and returns an integer. Spaces are allowed. Only the first number is returned:

Example

parseInt("-10");
parseInt("-10.33");
parseInt("10");
parseInt("10.33");
parseInt("10 20 30");
parseInt("10 years");
parseInt("years 10");

Try it yourself

If the number cannot be converted, it returns NaN (Not a Number, non-numeric).

parseFloat() method

parseFloat() parses a string and returns a number. Spaces are allowed. Only the first number is returned:

Example

parseFloat("10");
parseFloat("10.33");
parseFloat("10 20 30");
parseFloat("10 years");
parseFloat("years 10");

Try it yourself

If the number cannot be converted, it returns NaN (Not a Number, non-numeric).

Number object methods

TheseObject methodBelongs to Number Object:

Method Description
Number.isInteger() If the parameter is an integer, it returns true.
Number.isSafeInteger() If the parameter is a safe integer, it returns true.
Number.parseFloat() Convert a string to a number.
Number.parseInt() Convert a string to an integer.

Number methods cannot be used for variables

The above number methods belong to JavaScript Number object.

These methods can only be accessed like Number.isInteger().

Using X.isInteger() where X is a variable will result in an error:

TypeError: X.isInteger is not a function.

The Number.isInteger() method

If the parameter is an integer, the Number.isInteger() method returns true.

Example

Number.isInteger(10);
Number.isInteger(10.5);

Try it yourself

Number.isSafeInteger() method

Safe integers are integers that can be precisely represented as double-precision floating-point numbers.

If the parameter is a safe integer, the Number.isSafeInteger() method returns true.

Example

Number.isSafeInteger(10);
Number.isSafeInteger(12345678901234567890);

Try it yourself

Note

Safe integers are from -(253 to + (253 All integers of - 1)

This is safe: 9007199254740991. This is not safe: 9007199254740992.

Number.parseFloat() method

Number.parseFloat() parses a string and returns a number.

Spaces are allowed. Only the first number is returned:

Example

Number.parseFloat("10");
Number.parseFloat("10.33");
Number.parseFloat("10 20 30");
Number.parseFloat("10 years");
Number.parseFloat("years 10");

Try it yourself

If the number cannot be converted, it returns NaN (Not a Number, non-numeric).

Note

Number.parseInt() and Number.parseFloat() methods are the same as the global methods parseInt() and parseFloat().

It aims to modularize global methods (so that the same JavaScript code can be used more easily outside of the browser).

Number.parseInt() method

Number.parseInt() parses a string and returns an integer.

Spaces are allowed. Only the first number is returned:

Example

Number.parseInt("-10");
Number.parseInt("-10.33");
Number.parseInt("10");
Number.parseInt("10.33");
Number.parseInt("10 20 30");
Number.parseInt("10 years");
Number.parseInt("years 10");

Try it yourself

If the number cannot be converted, it returns NaN (Not a Number, non-numeric).

Complete JavaScript Number Reference Manual

For a complete reference manual, please visit our complete JavaScript Number Reference Manual.

The reference manual includes descriptions and examples of all properties and methods of the Number object.