The JavaScript number methods help to work on numbers. JavaScript number methods and properties help to work with primitive values. In JavaScript, numbers are implemented in double-precision 64-bit binary format IEEE 754. Numbers are only one type, represent numbers with or without decimals.
Following are some JavaScript number methods that are used to work with numbers in JavaScript:
The toPrecision() Method
The toPrecision()
the method is used to return a string, with a number written with a specified length:
The toExponential() Method
The toExponential() method is used to
return a string, with a number rounded and written using exponential notation. The number of characters behind the decimal point is defined using a parameter:
<!DOCTYPE html> <html> <body> <p id="tacode"></p> <script> let a = 8.789; document.getElementById("tacode").innerHTML = a.toExponential() + "<br>" + a.toExponential(2) + "<br>" + a.toExponential(4) + "<br>" + a.toExponential(6); </script> </body> </html>
Output:
The toFixed() Method
The toFixed() method is used to
return a string, with the number written with a specified number of decimals:
<!DOCTYPE html> <html> <body> <p id="tacode"></p> <script> let a = 8.789; document.getElementById("tacode).innerHTML = a.toFixed(0) + "<br>" + a.toFixed(1) + "<br>" + a.toFixed(2) + "<br>" + a.toFixed(3); </script> </body> </html>
Output:
The valueOf() Method
The valueOf()
the method is used to return a number as a number:
The toString() Method
The toString()
method is used to return a number as a string:
JavaScript Number Methods Properties
Following are some number properties that are used in JavaScript:
MIN_VALUE and MAX_VALUE
The MAX_VALUE
is used to return the largest possible number and the MIN_VALUE
is used to return the lowest possible number in JavaScript.
POSITIVE_INFINITY and NEGATIVE_INFINITY
The POSITIVE_INFINITY
and NEGATIVE_INFINITY
is used to return on overflow:
JavaScript NaN – Not a Number
In JavaScript NaN
is a reserved word which is used to show that a number is not a legal number.
let a = Number.NaN;
Trying to do arithmetic with a non-numeric string will result in NaN
:
The Number Properties Cannot be used on Variables because it belongs to JavaScript’s number object wrapper called Number.
Converting Variables to Numbers
Following are some JavaScript methods that can be used to convert variables to numbers. These are not number methods, but global JavaScript methods. The global methods can be used on all JavaScript data types.
1. The Number() Method
The Number()
is used to convert variables to numbers in JavaScript:
The NaN
is returned, if the number cannot be converted.
2. The Number() Method for Dates
The Number()
the method is also used to convert a date to a number:
3. The parseInt() Method
The parseInt()
the method is used to parse a string and return a whole number. In this method, only the first number is returned and spaces are allowed:
The NaN
is returned, if the number cannot be converted.
4. The parseFloat() Method
The parseFloat()
the method is used to parse a string and return a number. In this method, only the first number is returned and spaces are allowed:
The NaN
is returned, if the number cannot be converted.