JavaScript Number Object

Welcome to the JavaScript tutorial, we have covered JavaScript objects. Today we will talk about the JavaScript Number Object. Unlike other languages, the JavaScript Number is the only type the JavaScript have. Because, like other languages, JavaScript don’t have different data types for integer, short or long integer, and floating point numbers. You can use the JavaScript number to hold date, decimal or integer numbers as well.

JavaScript Number – Syntax

Now let’s see the syntax of number object:

Var sharplesson = new Number (value)

JavaScript number object is a wrapper that will hold the primitive values. Here you can see, we have created a number object ‘ sharplesson’ with the help of new Number () constructor. The value parameter is a numeric value of the object.

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.

JavaScript Number Properties

Constructor – This property will specify the function that created the number object.

MAX_VALUE – It will represent the largest numeric value in the JavaScript.

MIN_VALUE – It will represent the smallest numeric value in the JavaScript.

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.

JavaScript Number – Methods

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.

JavaScript Number – Examples

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.

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.

JavaScript will show the numbers as base 10 decimals.

You can change the results by using toString () function to show numbers as base 8 or so.

JavaScript numbers are a 64 bit floating point number.

You can’t compare objects.

Conclusion

We have just covered JavaScript number objects, its method and property. We tried to serve you easy to understand article. The only thing we expect from our reader is to do practice. Trust us, JavaScript is very easy to learn language. The practice will make you perfect in your basics to implement.