JavaScript
In this tutorial we will learn about JavaScript Number Object.
Number is one of the JavaScript object to work with numbers.
In the following example we a number object.
var numObj = new Number(10); console.log(typeof numObj); //this will print object
Following are some of the properties and methods of Number object that we can use.
This holds a constant value that represents the maximum value that a number object can hold before JavaScipt interprets it as positive infinity.
The following example we will print value of MAX_VALUE in browser console.
MAX_VALUE
console.log(Number.MAX_VALUE);
Output
1.7976931348623157e+308
This holds a constant value that represents the minimum value that a number object can hold before JavaScipt interprets it as negative infinity.
The following example we will print value of MIN_VALUE in browser console.
MIN_VALUE
console.log(Number.MIN_VALUE);
5e-324
This represents the value of positive infinity.
The following example we will print value of POSITIVE_INFINITY in browser console.
POSITIVE_INFINITY
console.log(Number.POSITIVE_INFINITY);
Infinity
This represents the value of negative infinity.
The following example we will print value of NEGATIVE_INFINITY in browser console.
NEGATIVE_INFINITY
console.log(Number.NEGATIVE_INFINITY);
-Infinity
This represents the value of "Not a Number".
The following example we will print value of NaN in browser console.
NaN
console.log(Number.NaN);
The best way to get this value is to divide a String by Number.
console.log("Hello World" / 10);
typeof NaN is number though NaN stands for "Not a Number"
typeof
number
console.log(typeof NaN); //this will print number
This method will return a string. The value of this string represents a rounded number having specified number of digits after the decimal.
In the following example we have a number object. We are using the toFixed() method to have 3 decimal places.
toFixed()
var num = new Number(123.4567890); console.log(num.toFixed(3)); //this will print 123.457
This method will return a string. The value of this string represents a rounded number having specified number of significant digit.
In the following example we have a number object. We are using the toPrecision() method to have 3 specified number of significant digit.
toPrecision()
var num = new Number(1.234567890); console.log(num.toPrecision(3)); //this will print 1.23
This method will return a string equal to the value of the Number object.
In the following example we have a number object. We are using the toString() method to get the string value.
toString()
var num = new Number(1.234567890); console.log(num.toString()); //this will print 1.23456789
This method will return a string which represent the number in exponential form.
In the following example we have a number object. We are using the toExponential() method to get the string value in exponential form.
toExponential()
var num = new Number(123.4567890); console.log(num.toExponential()); //this will print 1.23456789e+2