JavaScript Number object
In this tutorial you will learn how to work with various number constants and methods.
Number object contains various constants and methods used to work with numbers. Number value in JavaScript is equivalent to double precision 64-bit binary format.
Number Properties
The JavaScript Number object has a few properties that are useful in numeric comparisons.
- MAX_VALUE: The largest possible numeric value in JavaScript
- MIN_VALUE: The smallest possible numeric value in JavaScript
- NEGATIVE_INFINITY: A value that is less than MIN_VALUE
- NaN: The special “Not a Number” value
- POSITIVE_INFINITY: A value that is greater than MAX_VALUE
document.write(Number.MAX_VALUE);
Number Methods:
toExponential( )
The Number.toExponential() method returns a string representing the number in exponential notation. The methodtakes an optional integer parameter between 0 and 20 that represents the number of digits after the decimal point; if itis omitted, the method will use as many digits as needed to fully represent the number.
varmyNumber = 4309; document.write(myNumber.toExponential()); // will alert 4.309e+3 document.write(myNumber.toExponential(2)); // will alert 4.31e+3
toFixed( )
The Number.toFixed() method returns a string representing the number in decimal notation and has exactly thenumber of digits after the decimal specified by the optional integer parameter. If the parameter is not specified,it is treated as 0 and the number will be rounded to its nearest integer.
varmyNumber = 40.29; document.write(myNumber.toFixed()); // will alert 40 document.write(myNumber.toFixed(1)); // will alert 40.3 document.write(myNumber.toFixed(4)); // will alert 40.2900
toPrecision( )
The Number.toPrecision() method returns a string representing the specified number rounded to the number of significant digits specified by the parameter. The result can be either fixed-point or exponential notation as needed. Typically the parameter is expected to be an integer value between 1 and 21, though this can vary depending on implementation. Parameters outside of the allowed range will throw a range error. If no numeric parameter is specified, the method simply returns a string representation of the number and is the equivalent of calling the toString() method.
varmyNumber = 40.29; alert(myNumber.toPrecision()); // alerts 40.29 alert(myNumber.toPrecision(1)); // alerts 4e+1 (which is 40 in exponential notation) alert(myNumber.toPrecision(10)); // alerts 40.2900000000
isNaN()
The isNaN() method is used for checking if something is (not) a number.
let x = 34; console.log(isNaN(x)); console.log(!isNaN(x)); let str = "hi"; console.log(isNaN(str));
isFinite()
The isNaN() method is used for checking if something is finite.It returns false for NaN, Infinity, and undefined, and true for all other values:
let x = 3; let str = "finite"; console.log(isFinite(x)); console.log(isFinite(str)); console.log(isFinite(Infinity)); console.log(isFinite(10 / 0));
isInteger()
The isInteger() method is used for Checking if something is an integer.Unlike isNaN() and isFinite(), isInteger() has not been made global and we will have to refer to the Number object to use it. It really does what you think it would: it returns true if the value is an integer and false when it's not:
let x = 3; let str = "integer"; console.log(Number.isInteger(x)); console.log(Number.isInteger(str)); console.log(Number.isInteger(Infinity)); console.log(Number.isInteger(2.4));