# Numbers in JavaScript

JavaScript Numbers are dedicated to store the **numeric values** including integers, decimal values and exponential values as well. Numbers can hold **positive** as well as **negative** values.

- There are no such separate categories for integers and decimal values in JavaScript. Both are treated as Numbers only.
**Example**:`var x = 25; var y = 2.5;`

- Exponential values are used when you want to represent extremely large or small numbers. To write numbers in exponential form, letter
**e**or**E**is used.**Example:**

This represents 2.8333 raised to the power 10.`var z = 2.8333e10;`

### How JavaScript Numbers are stored in memory?

In JavaScript, a number occupies **64 bits** in the memory. This format is known as **64-bit floating point format** defined by **IEEE 754 standards**. This format is also termed as **Double Precision**.

Let us see how these 64 bits are distributed to different parts of the number.

- 0 to 51st bit hold the
**integer**or**decimal**value. - 52nd to 62nd bit hold the
**exponential**value. - 63rd bit is dedicated for the
**sign**, be it positive(+) or negative(-).

Since there is a limited amount of bits to store a number, therefore, only limited numbers can be represented in JavaScript. Maximum of **2 ^{64} **different numbers can be represented. Well, this is value is equivalent to

**18 quintillions**.

### Range of Numbers in JavaScript

In JavaScript, the numbers must lie between **-9007199254740992(-2 ^{53})** to

**9007199254740992(2**. A number going out of this range might lose its

^{53})**precision**i.e the number won't be very accurate.

### JavaScript Numbers as Objects

JavaScript Number is a primitive datatype but numbers can also be created as an object. For this purpose, a keyword **new** is used.

**Syntax**:

var variable_name = new Number(value);

**Example**:

```
<!DOCTYPE html>
<html lang="en">
<head> </head>
<body>
<script>
var age = new Number(56);
document.write('age = ', age);
</script>
</body>
</html>
```

**Output**:

`age = 56`

From the execution point of view, the number objects make the execution process very slow. Hence, they are avoided.

**Special Numbers in JavaScript**

JavaScript owns three special numbers. They are:

- Infinity
- -Infinity
- NaN

### Infinity in JavaScript

It is the same infinity you've heard about in Mathematics. The positive infinity is displayed when the number falls above the range of floating point numbers i.e **-1.797693134862315E+308** to **1.797693134862315E+308**.

The most common example is '*Division by Zero*' whose result is Infinity. **Example**:

```
<!DOCTYPE html>
<html lang="en">
<head> </head>
<body>
<script>
var num_1 = 150;
var num_2 = 0;
document.write( num_1 / num_2 );
</script>
</body>
</html>
```

**Output**:

`Infinity`

### -Infinity in JavaScript

Similar to infinity, negative infinity is displayed when a number falls below the range of floating-point numbers.

In this example, let us take a **negative** value as dividend.**Example**:

```
<!DOCTYPE html>
<html lang="en">
<head> </head>
<body>
<script>
var num_1 = -150;
var num_2 = 0;
document.write( num_1 / num_2 );
</script>
</body>
</html>
```

**Output**:

`-Infinity`

### NaN in JavaScript

NaN stands for '**Not a Number**', which is displayed by JavaScript when a number is **invalid**. JavaScript does not generate an error in cases like '*Zero divided by Zero*' or '*Number divided by String*', instead, it gives NaN as output.

Let us see an example that shows what kind of values are NaN.**Example**:

```
<!DOCTYPE html>
<html lang="en">
<head> </head>
<body>
<script>
document.write(0/0); //zero divided by zero.
document.write(60/'ten'); //number divided by string.
</script>
</body>
</html>
```

**Output**:

```
NaN
NaN
```