JavaScript Number()

Posted by


Introduction to the JavaScript Number() Constructor

The JavaScript Number() constructor is an object that allows you to create a number object from a literal value, a decimal, a binary, or an octal. This constructor is used to convert a value, either a string or a number, into a number object. It can also be used to represent infinity, not-a-number (NaN), and special numerical values.

Syntax of the JavaScript Number() Constructor

The syntax for the JavaScript Number() constructor is as follows:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

Number([value])

[/dm_code_snippet]

The parameter value can be an integer, a float, a string, or a Boolean. If no value is given, then the result will be 0.

Examples of the JavaScript Number() Constructor

Let’s look at some examples of using the JavaScript Number() constructor.

To create a number object from an integer, use the following syntax:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

var x = new Number(20);

[/dm_code_snippet]

To create a number object from a float, use the following syntax:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

var y = new Number(3.14);

[/dm_code_snippet]

To create a number object from a string, use the following syntax:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

var z = new Number("100");

[/dm_code_snippet]

To create a number object from a Boolean, use the following syntax:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

var a = new Number(true);

[/dm_code_snippet]

To create a number object from infinity, use the following syntax:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

var b = new Number(Infinity);

[/dm_code_snippet]

To create a number object from not-a-number (NaN), use the following syntax:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

var c = new Number(NaN);

[/dm_code_snippet]

Conclusion

In this tutorial, we discussed the JavaScript Number() constructor. We covered the syntax of the constructor and the different ways to create a number object from a literal value, a decimal, a binary, or an octal. We also discussed how to represent infinity, not-a-number (NaN), and special numerical values using the constructor.