Now let’s see the syntax of number object:
Var sharplesson = new Number (value)
Note: If the value is not a number or cannot be converted to a number, then result you will see is ‘NaN’. It means, it is not a number.
Constructor – This property will specify the function that created the number object.
NaN – It will represent, that the value is not a number.
NEGATIVE_INFINITY – It will represent the negative value in the infinity.
POSITIVE_INFINITY – It will represent the positive value in the infinity.
Prototype – It will allow to add properties and methods to an object or objects.
toExponential () – It will display the number in the exponential notation.
toFixed () – It will return a string, but in a fixed-point number notation.
toLocaleString () – IT will return the object, which will be converted to a string as per the current locale.
toPrecision () – It will return the string that has a specific number of digits.
toString () – It will return the string representation of the number object or other object.
valueOf () – It will return the primitive value of the number object.
Let’s understand the number object with examples. We have declared integer, float, and exponent values as well. Not only this, we will create an object ‘sharp’ and assigned a value of 3 to it.
<html> <body> <script> var a=14; var b=14.7; var c=1e4; var sharp=new Number(3); document.write(a+"<br> "+b+"<br> "+c+"<br> "+sharp); </script> </body> </html>
When you will run the above code, then you will see:
You can clearly see the integer value is 14, the float value is 14.7 and the exponential result is 10000. The last integer value is by the number object ‘sharp’.
Note: We have not discussed method or property. Because we feel, it requires a separate discussion on each and every method or property.
Points to remember
- Try to avoid the use of number object, because it will probably slow down the speed or show unwanted effects.
- You can change the results by using toString () function to show numbers as base 8 or so.
- You can’t compare objects.