JavaScript Number Methods
The toString() method returns a number as a string.
| Share Basic Methods Basic number methods can be used on any number: toString() toExponential() toFixed() toPrecision() valueOf() | Static Methods Static methods can only be used on Number: Number.isFinite() Number.isInteger() Number.isNan() Number.isSafeInteger() Number.parseInt() Number.parseFloat() |
|---|
The toString() Method
The toString() method returns a number as a string.
All number methods can be used on any type of numbers (literals, variables, or expressions):
Example
Example Code
javascriptlet x = 123;
x.toString();
(123).toString();
(100 + 23).toString();The toString() method can take an optional radix argument to convert the number to a different base:
Example
Example Code
javascriptlet x = 123;
let text = x.toString(2);The toExponential() Method
toExponential() returns a string, with a number rounded and written using exponential notation.
A parameter defines the number of characters behind the decimal point:
Example
Example Code
javascriptlet x = 9.656;
x.toExponential(2);
x.toExponential(4);
x.toExponential(6);The parameter is optional. If you don't specify it, JavaScript will not round the number.
The toFixed() Method
toFixed() returns a string, with the number written with a specified number of decimals:
Example
Example Code
javascriptlet x = 9.656;
x.toFixed(0);
x.toFixed(2);
x.toFixed(4);
x.toFixed(6);The toPrecision() Method
toPrecision() returns a string, with a number written with a specified length:
Example
Example Code
javascriptlet x = 9.656;
x.toPrecision();
x.toPrecision(2);
x.toPrecision(4);
x.toPrecision(6);The valueOf() Method
valueOf() returns a number as a number.
Example
Example Code
javascriptlet x = 123;
x.valueOf();
(123).valueOf();
(100 + 23).valueOf();In JavaScript, a number can be a primitive value (typeof = number) or an object (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.
Converting Variables to Numbers
There are 3 JavaScript methods that can be used to convert a variable to a number:
| Method | Description |
|---|---|
| Number() | Returns a number converted from its argument. |
| parseFloat() | Parses its argument and returns a floating point number |
| parseInt() | Parses its argument and returns a whole number |
The Number() Method
The Number() method can be used to convert JavaScript variables to numbers:
Example
Example Code
javascriptNumber(true);
Number(false);
Number("10");
Number(" 10");
Number("10 ");
Number(" 10 ");
Number("10.33");
Number("10,33");
Number("10 33");
Number("John");The Number() Method Used on Dates
Number() can also convert a date to a number.
Example
Example Code
javascriptNumber(new Date("1970-01-01"))The number of milliseconds between 1970-01-02 and 1970-01-01 is 86400000:
Example
Example Code
javascriptNumber(new Date("1970-01-02"))Example
Example Code
javascriptNumber(new Date("2017-09-30"))The parseInt() Method
parseInt() parses a string and returns a whole number. Spaces are allowed. Only the first number is returned:
Example
Example Code
javascriptparseInt("-10");
parseInt("-10.33");
parseInt("10");
parseInt("10.33");
parseInt("10 20 30");
parseInt("10 years");
parseInt("years 10");If the number cannot be converted, NaN (Not a Number) is returned.
The parseFloat() Method
parseFloat() parses a string and returns a number. Spaces are allowed. Only the first number is returned:
Example
Example Code
javascriptparseFloat("10");
parseFloat("10.33");
parseFloat("10 20 30");
parseFloat("10 years");
parseFloat("years 10");If the number cannot be converted, NaN (Not a Number) is returned.
Number Object Methods
These object methods belong to the Number object:
| Method | Description |
|---|---|
| Number.isInteger() | Returns true if the argument is an integer |
| Number.isNaN() | Returns true if the argument is NaN |
| Number.isFinite() | Returns true if the argument is not Infinity nor NaN |
| Number.isSafeInteger() | Returns true if the argument is a safe integer |
| Number.parseFloat() | Converts a string to a number |
| Number.parseInt() | Converts a string to a whole number |
The Number.isInteger() Method
The Number.isInteger() method returns true if the argument is an integer.
Example
Example Code
javascriptNumber.isInteger(10);
Number.isInteger(10.5);The Number.isFinite() Method
The Number.isFinite() method returns true if the argument is not Infinity, -iInfinity, nor Nan.
Example
Example Code
javascriptNumber.isFinite(123);The Number.isNaN() Method
The Number.isNaN() method returns true if the argument is NaN (Not a Number).
Example
Example Code
javascriptNumber.isNaN(123);The Number.isSafeInteger() Method
A safe integer is an integer that can be exactly represented as a double precision number.
The Number.isSafeInteger() method returns true if the argument is a safe integer.
Example
Example Code
javascriptNumber.isSafeInteger(10);
Number.isSafeInteger(12345678901234567890);The Number.parseFloat() Method
Number.parseFloat() parses a string and returns a number.
Spaces are allowed. Only the first number is returned:
Example
Example Code
javascriptNumber.parseFloat("10");
Number.parseFloat("10.33");
Number.parseFloat("10 20 30");
Number.parseFloat("10 years");
Number.parseFloat("years 10");If the number cannot be converted, NaN (Not a Number) is returned.
The Number.parseInt() Method
Number.parseInt() parses a string and returns a whole number.
Spaces are allowed. Only the first number is returned:
Example
Example Code
javascriptNumber.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");If the number cannot be converted, NaN (Not a Number) is returned.
The Number.isInteger() Method
The Number.isInteger() method returns true if the argument is an integer.
Example: isInteger()
Example Code
javascriptNumber.isInteger(10);
Number.isInteger(10.5);The Number.isSafeInteger() Method
A safe integer is an integer that can be exactly represented as a double precision number.
The Number.isSafeInteger() method returns true if the argument is a safe integer.
Example isSafeInteger()
Example Code
javascriptNumber.isSafeInteger(10);
Number.isSafeInteger(12345678901234567890);Interactive Code Examples
JS Example 1
javascriptlet x = 123;
x.toString();
(123).toString();
(100 + 23).toString();JS Example 2
javascriptlet x = 123;
let text = x.toString(2);JS Example 3
javascriptlet x = 9.656;
x.toExponential(2);
x.toExponential(4);
x.toExponential(6);JS Example 4
javascriptlet x = 9.656;
x.toFixed(0);
x.toFixed(2);
x.toFixed(4);
x.toFixed(6);JS Example 5
javascriptlet x = 9.656;
x.toPrecision();
x.toPrecision(2);
x.toPrecision(4);
x.toPrecision(6);JS Example 6
javascriptlet x = 123;
x.valueOf();
(123).valueOf();
(100 + 23).valueOf();