Unlike other programming languages in JavaScript, we only have one type

that represents all the numbers including integers, floats, real, decimal as well as complex.**number**

We can either use decimal points or simple integers to represent a number.

**Example **

let a = 20; // number without decimal let b = 20.1; // number with decimal

To represent a very small or big number we can use the exponent notation in JavaScript.

let l = 21e6; // 21*1000000 let s = 21e-6; // 21/1000000

The `e`

suffix after the number represents the exponent power.

**JavaScript 64-bit Floating Points Number**

Unlike other programming languages, we do not get dedicated keywords to represent float, integer, short, decimal, or double numbers in JavaScript.

JS follows the international IEEE754 Standard to store all the numbers as double-precision floating-point numbers.

According to the format JS use 64 bits to store a number, out 0f 64 bits 0 to 51 bits assigned to the number or fraction part, 52 to 62 bits allocated to the exponent part and 63 bit allocated tot the sign.

Number | Exponent | Sign |
---|---|---|

0-52 (52 bits) | 52 – 62 (11 bits ) | 63 (1 bit) |

If we talk about the precision of numbers represented by JavaScript, JS only precise the numbers with or without decimals accurately that are 15 digits long.

**Example **

let a = 999999999999999; // 999999999999999 let b = 9999999999999999; // 10000000000000000 let c = 1.999999999999999; // 1.999999999999999 let d = 1.9999999999999999; // 2

If a number contains more than 15 digits, JS will not be able to maintain the number precision.

**JavaScript Numbers with String **

JS numbers show a very unique nature when operating them with JavaScript string. The JS engine takes care of all the implicit type conversions and returns an appropriate result.

**Adding numbers and string**

The `+`

operator can be used to add two numbers or concatenate two strings. But if we use the + operator between a number and a string then it will convert the number into a string and concatenate them.

**Example**

let a = 20; // number let b = "21"; // string let c = a + b; // 2021 string

Whenever we use the number with string along with `+`

operator, it will always treat the number as a string and perform the concatenate operation, not addition.

**Example**

let a = 20; // number let b = 20; // number let c = "Concatenate a + b " + a+b; // "Concatenate a + b 2020"

In the above example both `a`

and `b`

were numbers still the + operator performs the concatenation operation, not added because the statement before a+b was a string so the JS engine converts the complete statement as a string.

Let’s say you do not want to perform concatenation when both the variables are numbers and the rest of the statement is a string, there we can use the brackets, so the interpreter first executes the bracket and all of its operations then, the rest statement.

let a = 20; // number let b = 20; // number let c = "Concatenate a + b " + (a+b); //"Concatenate a + b40"

### Other Arithmetic operation between string and numbers

Only the `+`

operator performs the concatenation operation between the string and a number, by implicit conversion of the number to a string. For the rest of the JS arithmetic operators, the JS interpreter converts the string into a corresponding integer and performs the arithmetic operation.

**Example**

let a = 20; //number let b ="2"; // string let mul = a*b; //40 (number) let div = a/b; //10 (number) let modu = a%b; // 0 (number) let sub = a-b; // 18 (number)

Even if we apply the arithmetic operators (except +) between two string numeric values, first both the string values will be converted into numbers and computed according to the operation.

let a = "20"; //string let b ="2"; // string let mul = a*b; //40 (number) let div = a/b; //10 (number) let modu = a%b; // 0 (number) let sub = a-b; // 18 (number)

**JavaScript NaN**

`NaN`

in JavaScript is a reserved keyword to represent Not a Number. NaN is generally used to indicate illegal number operations such as when we perform arithmetic operations between a non-numeric string and a number.

**Example**

let a = 20; // number let b = "twenty"; // string let c = a*b; // NaN

Unlike other programming languages, JS does not throw an `TypeError`

when we perform an arithmetic operation between a number and string or string and string, instead, it returns a new value NaN representing Not a Number.

**Example **

let a = "one"; // string let b = "two"; // string let c = a*b; //NaN

**JavaScript Infinity**

JavaScript provides a dedicated `Infinity`

keyword to represent an infinite number. The Infinite number can be positive `Infinity`

or negative `-Infinity`

.

Because of this keyword, we do not have ZeroDivision Error in JavaScript.

**Example **

let a = Infinity; let b = 20; let c = a+b; // Infinity

**Example **

let a = 20; let b = 0; let c = a/b; // Infinity

### JavaScript HexaDecimal

In JavaScript, we also represent numbers in HexaDecimal format. To create a HexaDecimal number we can use the `0x`

prefix followed by the HexaDecimal representation of a number.

**Example**

let hex = 0xAB; // 171

**JavaScript Number Objects**

By far we have been declaring or defining numbers as a primitive data type, but JavaScript also provides a built-in `Number()`

object, which can also be used to represent the Number data type.

**Example**

let p_num = 200; // 200 primitive data type let o_num = new Number(200); // 200 Object number

In the above example both `p_num`

and `o_num`

represent the same value, and both can perform most of the similar tasks. But it always suggests using the primitive number data type unless you know why you are using the `Number()`

object.

## Summary

- Numbers are used to representing numeric values.
- Number support all the arithmetic operations.
- + operator performs the contamination operation between string-number and string-string.
- NaN is a Not a Number value that represents illegal numbers.
- Infinity and -Infinity represent the largest and smallest numbers.
- Number() objects can also be used to represent numbers.

**People are also reading:**