Number()

The Number() constructor function is used to create numeric objects and numeric primitive values.

In the following sample, I detail the creation of numeric values in JavaScript.

Sample: sample49.html


Integers and Floating-Point Numbers

Numbers in JavaScript are typically written as either integer values or floating-point values. In the following code, I create a primitive integer number and a primitive floating-point number. This is the most common usage of number values in JavaScript.

Sample: sample50.html

A numeric value can be a hexadecimal value or octal value in JavaScript, but this is typically not done.


Number() Parameters

The Number() constructor function takes one parameter: the numeric value being created. In the following snippet, we create a number object for the value 456 called numberOne.

Sample: sample51.html

When used with the new keyword, instances from the Number() constructor produce a complex object. You should avoid creating number values using the Number() constructor (use literal/primitive numbers) due to the potential problems associated with the typeof operator. The typeof operator reports number objects as 'object' instead of the primitive label ('number') you might expect. The literal/primitive value is just more concise.


Number() Properties

The Number() object has the following properties:

Properties (e.g., Number.prototype;)


Number Object Instance Properties and Methods

Number object instances have the following properties and methods (not including inherited properties and methods):

Instance Properties (e.g., var myNumber = 5; myNumber.constructor;)

Instance Methods (e.g., var myNumber = 1.00324; myNumber.toFixed();)


Conclusion

The Number() constructor will surely be useful to you in your JavaScript adventures.

Tags:

Comments

Related Articles