JavaScript Number Object

Number Object

Number object in JavaScript is used to represent numeric values. Unlike other programming languages, JavaScript does not provide different types of numbers, like integer, float, short etc. In JavaScript numbers are always stored as double precision floating point numbers. Numbers are stored in 64 bit format, where the fraction is stored in bits 0 to 51, the exponent in bits 52 to 62 and sign in bit 63. The number object is created as follows:
  1. var value = new Number(numberValue);  
In the place of numberValue, if you provide any non-numeric parameter, it returns NaN.

In JavaScript you don’t need to create number object like above syntax because the numeric literals are treated as instances of Number class.

Note:

Don’t create Number objects. It slows down execution speed. The “new” keyword complicates the code. This can produce some unexpected results as below:

If you create numbers as:
  1. var x = 100;  
  2. var y = new Number(100);  
When we compare these x and y using “==” operator it returns true.

But when we use “===” operator it returns false. Because this operator checks both value and type.

If you create both numbers using “new” keyword, the “==” operator returns false because objects in JavaScript cannot be compared. So don’t create Number objects.

Hexadecimal:

JavaScript interprets numeric constants if they are prefixed by “0x”. 
  1. var x=0xFF;   
Here the value of x will be 255.

Number Properties

The following are the properties of Number.

 Property  Description
 MAX_VALUE Returns largest possible value in JavaScript, 1.7976931348623157E+308
MIN_VALUE Returns smallest possible value in JavaScript, 5E-324
NaN Represents value that is not number
NEGATIVE_INFINITY Represents value that is less than MIN_VALUE
POSITIVE_INFINITY Represents value that is greater than MAX_VALUE

You can access above properties as below, 
  1. var value=Number. MAX_VALUE;  
Number Methods

The following are the Number methods in JavaScript.
  1. toExponential():

    This method returns the string that represent given number in exponential format. It takes one optional parameter, an integer specifying the number of digits after the decimal points.

    Syntax: x.toExponential(fractiondigits);

    Ex:

    1. var x=10;  
    2. x.toExponential(); // Returns 1e+1  
    3. x.toExponential(2); // Returns 1.00e+1  
  2. toFixed():

    This method format a number with specific number of digits to the right of decimal point. It takes one optional parameter that represents the number of digits to appear after decimal points. If parameter is not given, it is treated as 0.

    Syntax: x.toFixed(digits);

    Ex:

    1. var x=10.646;  
    2. x. toFixed (); // Returns 11  
    3. x. toFixed (2); // Returns 10.65  
  3. toPrecision():

    This method returns a string, with a number written with a specified length. It takes one optional parameter that defines the number of digits (including digits to the left and right of decimal point) to be displayed.

    Syntax: x.toPrecision(digits);

    Ex:

    1.  var x=10.1111;  
    2. x. toPrecision (); // Returns 10.1111  
    3. x. toPrecision (2); // Returns 10  
    4. x. toPrecision (3); // Returns 10.1  
  4. toString():

    This method returns the string representation of number. It takes one optional parameter specifying the base for representing the numeric value. The parameter value should be between 2 to 36. If the parameter is not specified then the base is assumed to be 10.

    Syntax: x.toString(radix);

    Ex:

    1. var x=6;  
    2. x. toString(); // Returns 6  
    3. x. toString(2); // Returns 110  
  5. valueOf():

    This method returns the value of number.

    Syntax: x.valueOf();

    Ex: 
    1. var x=123;  
    2. x.valueOf(); // Returns 123  
    3. (123).valueOf(); // Returns 123  
    4. (1+3).valueOf() // Returns 4