JavaScript Number Methods
Basic MethodsBasic number methods can be usedon any number: toString()toExponential() toFixed() toPrecision() valueOf() | Static MethodsStatic methods can only be usedon Number: Number.isFinite()Number.isInteger() Number.isNan() Number.isSafeInteger() Number.parseInt() Number.parseFloat() |
The toString() Method
ThetoString() method returns a number as a string.
All number methods can be used on any type of numbers (literals, variables, or expressions):
ThetoString() method can take an optional radix argument to convertthe number to a different base:
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:
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:
toFixed(2) is perfect for working with money.
The toPrecision() Method
toPrecision() returns a string, with a number written with a specified length:
Example
x.toPrecision();
x.toPrecision(2);
x.toPrecision(4);
x.toPrecision(6);
The valueOf() Method
valueOf() returns a number as a number.
In JavaScript, a number can be a primitive value (typeof = number) or an object (typeof = object).
ThevalueOf() method is used internally in JavaScript to convert Number objects to primitive values.
There is no reason to use it in your code.
All JavaScript data types have avalueOf() and atoString() method.
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 methods above are notnumber methods. They areglobal JavaScript methods.
The Number() Method
TheNumber() method can be used to convert JavaScript variables to numbers:
Example
Number(false);
Number("10");
Number(" 10");
Number("10 ");
Number(" 10 ");
Number("10.33");
Number("10,33");
Number("10 33");
Number("John");
If the number cannot be converted,NaN (Not a Number) is returned.
The Number() Method Used on Dates
Number() can also convert a date to a number.
Note
TheDate() method returns the number of milliseconds since 1.1.1970.
The number of milliseconds between 1970-01-02 and 1970-01-01 is 86400000:
The parseInt() Method
parseInt() parses a string and returns a whole number. Spaces are allowed. Only the first number is returned:
Example
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
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
Theseobject methods belong to theNumber 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 |
Number Methods Cannot be Used on Variables
The number methods above belong to the JavaScriptNumber Object.
These methods can only be accessed likeNumber.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
TheNumber.isInteger() method returnstrue if the argument is an integer.
The Number.isFinite() Method
TheNumber.isFinite() method returnstrueif the argument is not Infinity, -iInfinity, nor Nan.
The Number.isNaN() Method
TheNumber.isNaN() method returnstrueif the argument is NaN (Not a Number).
Note
Number.isNaN() is the preferred way to check for equality with NaN.You cannot test for equality with NaN using == or ===.
The Number.isSafeInteger() Method
A safe integer is an integer that can be exactly represented as a double precision number.
TheNumber.isSafeInteger() method returnstrue if the argument is a safe integer.
Safe integers are all integers from -(253 - 1) to +(253 - 1).
This is safe: 9007199254740991. This is not safe: 9007199254740992.
The 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.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.
Note
TheNumber methodsNumber.parseInt()andNumber.parseFloat()
are the same as the
Global methodsparseInt() andparseFloat().
The purpose of these two methods is modularization of globals, to make it easier to use the sameJavaScript code outside the browser.
The Number.parseInt() Method
Number.parseInt() parses a string and returns a whole number.
Spaces are allowed. Only the first number is returned:
Example
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
TheNumber.isInteger() method returnstrue if the argument is an integer.
The Number.isSafeInteger() Method
A safe integer is an integer that can be exactly represented as a double precision number.
TheNumber.isSafeInteger() method returnstrue if the argument is a safe integer.
Example isSafeInteger()
Number.isSafeInteger(12345678901234567890);
Note
Safe integers are all integers from -(253 - 1) to +(253 - 1).
This is safe: 9007199254740991. This is not safe: 9007199254740992.
Complete JavaScript Reference
For a complete reference to all JavaScript properties and methods, with full descriptions and many examples, go to:
W3Schools' Full JavaScript Reference.
The reference inludes all JavaScript updates from 1999 to 2025.

