JavaScript Number Methods

18 JavaScript Number Methods, Simplified examples

0 / 0 8 Mins Admin

October 31, 2022



The JavaScript number methods help you to work with numbers. JavaScript can work with various numeric values like integer, float, hexadecimal, decimal, exponential and octal values.

We will explain Javascript number methods in a simple manner with examples.

JavaScript Number Methods
Photo by Atypeek Dgn on

The toString() method.

This method converts the number into a string or returns a number as a string.

var x = 123;

// x is number
//output: "number"

var y = x.toString()

// convert to string
//output: "string"

The toExponential() Method

Exponential notation is a method of writing numbers. 

Exponential numbers take the form Xn, where X is multiplied by itself n times.

A simple example is 8 = 23 = 2×2×2.

In JavaScript toExponential() method returns a string with a rounded number written in exponential form.

var x = 323.34;
var y = x.toExponential();
//output : 3.2334e+2

The toFixed() Method

The toFixed() method converts a given number into a string. It rounds to a specified number of decimals. Additional zeros are added to create the desired decimal length if the number of decimals is higher than the actual number.

var x = 323.3433;
var y = x.toFixed(2);
//same as
//x.toFixed(0);           // returns 4
//x.toFixed(2);           // returns 3.66
//output: 323.34
//Example: if the given length in parameters is larger
var x = 3.656;
// output: 3.6560
//output:  3.656000

The toPrecision() Method

The toPrecision() method formats a number to a specified length with the given number of significant digits. It rounds the result where necessary. This method returns the value as a string.

Here is a simple example

var x = 7.938;
x.toPrecision();        // output 7.938
x.toPrecision(2);       // output 7.9
x.toPrecision(4);       // output  7.938
x.toPrecision(6);       // output 7.93800

The valueOf() Method

The valueOf() method returns the primitive value of a String object.

This method is called internally by JavaScript, and not explicitly in the code. All JavaScript data types have a valueOf() and a toString() method.

var x = 444;
x.valueOf();            // output 444 from variable x

How to convert String variables to Numeric in JavaScript?

There are 3 JavaScript methods that can be used to convert string variables to numbers.
Number() method, parseInt() method and parseFloat() method

JavaScript global methods 

The methods mentioned above are JavaScript global methods. this includes:

  • Number() Method
  • parseInt() method
  • parseFloat() method

Number() Method

The Number() method is used to convert JavaScript variables to numbers.

consider the following example

// Example

// output: 1
// output: 0
// output: 20
Number(" 20  ");
// output: 20
// output: 20.43
// output:  NaN (Not a Number)
Number("20 33");
// output: NaN
Number("John Doe");
// output: NaN


The Number() method is also useful to convert Dates to numbers. It returns the number of milliseconds since 1-1-1970.

// Example

let dateToNumber  = Number(new Date("2021-01-01"));
// output: 1609459200000


parseInt() method

parseInt() parses a string and returns a whole number. Use spaces as parameters but it returns only the first number.

This method returns NaN ( not a number ) if the given string is impossible to convert to a number.

// Example

// output: 20
//output: 20
parseInt("20 70");
// output: 20
parseInt("20 Days");
// output: 20
parseInt("Date 20");
// output: NaN

parseFloat() Method

The parseFloat() Method in JavaScript parses a string and returns a floating-point number. It also returns NaN if the given string is impossible to convert to a string.

// Example

// output: 20
// output: 20.71
parseFloat("20 10");  
// output: 20
parseFloat("20 Days");  
// output: 20
parseFloat("Number 20");  
// output: NaN


Conclusion on JavaScript Number Methods

The above topic covers all the fundamental topics about JS methods and functions of Numbers

You can represent numeric dates by the Number object, which can be either integers or floating-point numbers.

The Number object in JavaScript is a wrapper for primitive numeric values.

There is only one kind of number data type in JavaScript, and it does not differentiate between integers and floating-point values.

Download “JavaScript Number Methods” as a PDF

All JavaScript number Methods in one video