JavaScript Numbers


JavaScript Number object allows us to represent a numeric value. It can have an integer, float, hexadecimal, octal or exponential value.

We can create a JavaScript Number object by the Number() constructor.

Syntax:

    var num = new Number(value);


Example:

    var num1 = 175;
    var num2 = 122.76;    


Note: If value can't be converted to number, it returns NaN(Not a Number) that can be checked by isNaN() method.


JavaScript Number Constants

Constant Description
MIN_VALUE Returns the minimum number value
MAX_VALUE Returns the maximum number value
POSITIVE_INFINITY Returns Positive infinity
NEGATIVE_INFINITY Returns Negative infinity
NaN Not a Number


JavaScript Number Methods

Method Description
isFinite() Returns true if number is finite else returns false.
Example: Number.isFinite(75); // returns TRUE
isNaN() Returns true if number is Not a Number else returns false.
Example: var num = 107;
isNaN(num); // returns TRUE
toString() Returns a number as a string.
Example: var num = 125;
num.toString(); // returns “125”
toFixed() Returns number with specified number of decimals as a string.
Example: var num = 19.628;
num.toFixed(2); // returns “19.63”
toExponential() Returns exponential value as a string.
Example: var num = 1000;
num.toExponential(); // returns “1.000e+3”
toPrecision() Returns number with specified length.
Example: var num = 9.628;
num.toPrecision(); // returns “9.7”
valueOf() Returns value of Number object.
Example: var num = 128;
num.valueOf(); // returns “128”
isInteger() Returns true if the number is an integer else returns false.