Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From \$16.50/m
Advertisement

# Number()

Difficulty:BeginnerLength:ShortLanguages:
This post is part of a series called JavaScript Succinctly.
String()
Boolean()

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;)

• MAX_VALUE
• MIN_VALUE
• NaN
• NEGATIVE_INFINITY
• POSITIVE_INFINITY
• 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;)

• constructor

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

• toExponential()
• toFixed()
• toLocaleString()
• toPrecision()
• toString()
• valueOf()

## Conclusion

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

Advertisement
Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.