JavaScript Number Methods


You can apply these specific actions to all numbers in JavaScript:

MethodDescription
toString()Returns a number as a string
toExponential()Returns a number written in exponential notation
toFixed()Returns a number written with a number of decimals
toPrecision()Returns a number written with a specified length
ValueOf()Returns a number as a number

The toString() Method

The toString() function changes a number into a string.

You can use number methods on any kind of numbers, whether they're written directly in your code, stored in variables, or created as part of mathematical expressions.


The toExponential() Method

The toExponential() function gives you a string that represents a number. This string shows the number in a rounded and exponential format.

A parameter defines the number of characters behind the decimal point:

The parameter is not required. If you don't mention it, JavaScript won't round the number.


The toFixed() Method

The toFixed() function provides a way to turn a number into a string. You can also decide how many decimal places you want in the result.

toFixed(2) is perfect for working with money.


The toPrecision() Method

The function toPrecision() gives you a string that represents a number with a specific length.


The valueOf() Method

valueOf() returns a number as a number.

In JavaScript, a number can be simple (like a basic number) or it can be more complex (like an object).

The valueOf() function is an internal tool in JavaScript. It helps change Number objects into simple values.

You don't need to include it in your code.

In JavaScript, every data type has a valueOf() and a toString() method.


Converting Variables to Numbers

Three JavaScript methods can change a variable into 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 not number methods. They are global JavaScript methods.


The Number() Method

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

If the conversion fails, it returns NaN (Not a Number).


The Number() Method Used on Dates

Number() can change a date into a numerical value.

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

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

The parseInt() Method

The parseInt() function reads a text and gives back a whole number. You can have spaces in the text. It only takes the first number from the text.

If the value can't be changed into a number, it will give back 'NaN' (which means Not a Number).


The parseFloat() Method

The parseFloat() function takes a string and gives back a number. It can handle spaces in the string. However, it only gives the first number it finds in the string.

If the number cannot be converted, NaN (Not a Number) is returned.


Number Object Methods

These methods are associated with the Number object:

MethodDescription
Number.isInteger()Returns true if the argument is an integer
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 JavaScript Number Object.

These methods are accessible using syntax like Number.isInteger().

Using X.isInteger() on a variable X will cause an error:

TypeError X.isInteger is not a function.


The Number.isInteger() Method

The Number.isInteger() function tells us if the given value is a whole number and returns true if it is.


The Number.isSafeInteger() Method

A safe integer is a whole number that can be accurately shown as a double precision number.

The Number.isSafeInteger() function checks if a number is a safe integer and returns true if it is.

Safe integers range from -(253 - 1) to +(253 - 1).
This is safe: 9007199254740991. This is not safe: 9007199254740992.


The Number.parseFloat() Method

The Number.parseFloat() function reads a text and gives back a numerical value.

Spaces are allowed. Only the first number is returned:

If the value can't be changed into a number, the result will be NaN, which stands for Not a Number.

Note : The Number methods Number.parseInt() and Number.parseFloat()

are the same as the

Global methods parseInt() and parseFloat().

The aim is to organize global elements into separate modules, allowing the JavaScript code to be used more easily outside the browser.


The Number.parseInt() Method

The function Number.parseInt() takes a string and converts it into a whole number.

Spaces are allowed. Only the first number is returned:

If the input number cannot be changed into a valid number, the result will be NaN, which stands for Not a Number.