JavaScript Number Object With Example

JavaScript Number Object

The Number object represents numerical date, either integers or floating-point numbers. The browser automatically converts number literals to instances of the number class.

Syntax for creating a number object:

        
  var val = new Number(number);
    

If the argument cannot be converted into a number, it returns NaN(Not-a-Number).

Number Properties

Property Description
MAX_VALUE Returns the maximum number value supported in JavaScript and largest number possible is (1.7976931348623157E+308)
MIN_VALUE Returns the smallest number value supported in JavaScript and smallest number possible is (5E-324)
NEGATIVE_INFINITY Returns negative infinity (-Infinity)
NaN Represents a value that is not a number.
POSITIVE_INFINITY Represents positive infinity (Infinity).

Number Methods:

Method Description
toExponential(fractionDigits) Returns exponential value as a string. Example:
var num = 100; Num.toExponential(2); // returns '1.00e+2'
toFixed(fractionDigits) Returns string of decimal value of a number based on specified fractionDigits. Example: var num = 100; Num.toFixed(2); // returns '100.00'
toLocaleString() Returns a number as a string value according to a browser's locale settings. Example: var num = 100; Num.toLocaleString(); // returns '100'
toPrecision(precisionNumber) Returns number as a string with specified total digits. Example: var num = 100; Num.toPrecision(4); // returns '100.0'
toString() Returns the string representation of the number value. Example: var num = 100; Num.toString(); // returns '100'
valueOf() Returns the value of Number object. Example: var num = new Number(100); Num.valueOf(); // returns '100'


Read Also: