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:
- 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:
- var x = 100;
- 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”.
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,
- var value=Number. MAX_VALUE;
Number Methods
The following are the Number methods in JavaScript.
- 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:
- var x=10;
- x.toExponential();
- x.toExponential(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:
- var x=10.646;
- x. toFixed ();
- x. toFixed (2);
- 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:
- var x=10.1111;
- x. toPrecision ();
- x. toPrecision (2);
- x. toPrecision (3);
- 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:
- var x=6;
- x. toString();
- x. toString(2);
- valueOf():
This method returns the value of number.
Syntax: x.valueOf();
Ex:
- var x=123;
- x.valueOf();
- (123).valueOf();
- (1+3).valueOf()