Javascript Number Methods

Let’s see the below list of JavaScript number methods with their description.

MethodsDescription
isFinite()It determines whether the given value is a finite number.
isInteger()It determines whether the given value is an integer.
parseFloat()It converts the given string into a floating point number.
parseInt()It converts the given string into an integer number.
toExponential()It returns the string that represents exponential notation of the given number.
toFixed()It returns the string that represents a number with exact digits after a decimal point.
toPrecision()It returns the string representing a number of specified precision.
toString()It returns the given number in the form of string.

toString() Method

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

All number methods might be used on any type of numbers (literals, variables, or expressions):

var x = 456;
x.toString();            // returns 456 from variable x
(456).toString();        // returns 456 from literal 456
(100 + 25).toString();   // returns 125 from expression 100 + 25

toExponential() Method

toExponential() returns a string, with a number that is rounded and written using exponential notation.

var x = 9.656;
x.toExponential(2);     // returns 9.66e+0
x.toExponential(4);     // returns 9.6560e+0
x.toExponential(6);     // returns 9.656000e+0

The toFixed() Method

toFixed() returns a string, with specified number of decimals:

var x = 8.656;
x.toFixed(0);           // returns 9
x.toFixed(2);           // returns 8.66
x.toFixed(4);           // returns 8.6560
x.toFixed(6);           // returns 8.656000

Note:- toFixed(2) is perfect for working with money.


toPrecision() Method

toPrecision() returns a string, with a specified length:

var x = 8.656;
x.toPrecision();        // returns 8.656
x.toPrecision(2);       // returns 8.7
x.toPrecision(4);       // returns 8.656
x.toPrecision(6);       // returns 8.65600

valueOf() Method

valueOf() returns a number as a number.

var x = 456;
x.valueOf();            // returns 456 from variable x
(456).valueOf();        // returns 456 from literal 456
(100 + 25).valueOf();   // returns 125 from expression 100 + 25

Note:- All JavaScript data types have a valueOf() and a toString() method.


Converting Variables to Numbers

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

  • The Number() method
  • The parseInt() method
  • The parseFloat() method

These methods are not number methods, but global JS methods.


Number() Method

Number() can be used to convert variables to numbers:

Number(true);          // returns 1
Number(false);         // returns 0
Number("11");          // returns 11
Number("  11");        // returns 11
Number("11  ");        // returns 11
Number(" 11  ");       // returns 11
Number("11.33");       // returns 11.33
Number("11,33");       // returns NaN
Number("11 33");       // returns NaN
Number("Daniel");        // returns NaN

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


Number() Method Used on Dates

Number(new Date("2020-04-28"));    // returns 1588032000000

parseInt() Method

parseInt() parses a string and returns a whole number. Spaces are allowed. Only the first number will be returned:

parseInt("20");         // returns 20
parseInt("20.33");      // returns 20
parseInt("20 30 40");   // returns 20
parseInt("20 years");   // returns 20
parseInt("years 20");   // returns NaN

parseFloat() Method

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

parseFloat("20");        // returns 20
parseFloat("20.33");     // returns 20.33
parseFloat("20 30 40");  // returns 20
parseFloat("20 years");  // returns 20
parseFloat("years 20");  // returns NaN

Number Properties Cannot be Used on Variables

Number properties belongs to the JavaScript’s number object called Number.

These properties can only be accessed or used as Number.MAX_VALUE.

Using myNumber.MAX_VALUE, where myNumber is a variable, expression, or value, will return undefined:

var x = 7;
var y = x.MAX_VALUE;    // y becomes undefined