Categories

# Number Type

##### Arrays

The simplest number is an integer. If you assign 1 to a variable and then use the typeof operator, it will return the string “number”.

Examples:

``````var n = 1;
typeof n;
//"number"

var n = 1234;
typeof n;
//"number"``````

Numbers can also be floating point (decimals):

``````var n = 1.23;
typeof n;
//"number"``````

You can call typeof directly on the value, without assigning it to a variable first:

``````typeof 123;
//"number"``````

#### Number functions

JavaScript includes a built-in Number function for converting values to numbers. To use the Number function, simply put the value (or a variable holding the value) that you want to convert to a number between the parentheses after the Number function.

#### The Number function produces four kinds of output:

The Boolean value true returns the number 1, like this:

``Number(true) // returns 1``

The Boolean value false returns the number 0, like this:

``Number(false) // returns 0``

Text strings that can’t be converted to numbers return the value NaN,

like this:

``Number("eggs") // returns NaN``

Numbers that are formatted as text strings are converted to numbers that can be used for calculations, like this:

``Number("42") // returns the number 42``

#### The parseInt() function

To JavaScript, all numbers are actually floating point numbers. However, you can use the parseInt() function to tell JavaScript to consider only the non fractional part of the number (the integer), discarding everything after the decimal point.

``parseInt(100.33); // returns 100``

#### The parseFloat() function

You can use parseFloat() to specifically tell JavaScript to treat a number as a float. Or, you can even use it to convert a string to a number.

For example:

``````parseFloat("10"); // returns 10
parseFloat(100.79); //returns 100.79
parseFloat("10"); //returns 10``````