JavaScript has just one kind of number. Numbers can be composed with or without decimals.

1 2 |
var x = 3.14; // A number with decimals var y = 3; // A number without decimals |

Additional enormous or additional little numbers can be composed with logical (example) documentation:

1 2 |
var x = 123e5; // 12300000 var y = 123e-5; // 0.00123 |

## JavaScript Numbers are Always 64-piece Floating Point

In contrast to numerous other programming dialects, JavaScript doesn’t characterize various kinds of numbers, similar to whole numbers, short, long, skimming point and so on.

JavaScript numbers are constantly put away as twofold accuracy skimming point numbers, keeping the universal IEEE 754 standard.

This configuration stores numbers in 64 bits, where the number (the division) is put away in bits 0 to 51, the example in bits 52 to 62, and the sign in bit 63:

Value (aka Fraction/Mantissa) | Exponent | Sign |
---|---|---|

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

## Accuracy

Whole (numbers without a period or example documentation) are precise up to 15 digits.

1 2 |
var x = 999999999999999; // x will be 999999999999999 var y = 9999999999999999; // y will be 10000000000000000 |

The most extreme number of decimals is 17, yet gliding point math isn’t constantly 100% precise:

1 |
var x = 0.2 + 0.1; // x will be 0.30000000000000004 |

To take care of the issue above, it increases and separation:

1 |
var x = (0.2 * 10 + 0.1 * 10) / 10; // x will be 0.3 |

## Including Numbers and Strings

Cautioning !!

JavaScript utilizes the + administrator for both expansion and link.

Numbers are included. Strings are linked.

On the off chance that you include two numbers, the outcome will be a number:

1 2 3 |
var x = 10; var y = 20; var z = x + y; // z will be 30 (a number) |

On the off chance that you include two strings, the outcome will be a string link:

1 2 3 |
var x = "10"; var y = "20"; var z = x + y; // z will be 1020 (a string) |

In the event that you include a number and a string, the outcome will be a string link:

1 2 3 |
var x = 10; var y = "20"; var z = x + y; // z will be 1020 (a string) |

On the off chance that you include a string and a number, the outcome will be a string link:

1 2 3 |
var x = "10"; var y = 20; var z = x + y; // z will be 1020 (a string) |

A typical error is to anticipate that this outcome should be 30:

1 2 3 |
var x = 10; var y = 20; var z = "The result is: " + x + y; |

A typical misstep is to anticipate that this outcome should be 102030:

1 2 3 4 |
var x = 10; var y = 20; var z = "30"; var result = x + y + z; |

The JavaScript translator works from left to right.

Initial 10 + 20 is included on the grounds that x and y are the two numbers.

At that point 30 + “30” is connected on the grounds that z is a string.

## Numeric Strings

JavaScript strings can have numeric substance:

1 2 3 |
var x = 100; // x is a number var y = "100"; // y is a string |

JavaScript will attempt to change over strings to numbers in every numeric activity:

This will work:

1 2 3 |
var x = "100"; var y = "10"; var z = x / y; // z will be 10 |

This will also work:

1 2 3 |
var x = "100"; var y = "10"; var z = x * y; // z will be 1000 |

What’s more, this will work:

1 2 3 |
var x = "100"; var y = "10"; var z = x - y; // z will be 90 |

In any case, this won’t work:

1 2 3 |
var x = "100"; var y = "10"; var z = x + y; // z will not be 110 (It will be 10010) |

In the last model JavaScript utilizes the + administrator to link the strings.

## NaN – Not a Number

NaN is a JavaScript saved word showing that a number is certifiably not a lawful number.

Attempting to do math with a non-numeric string will bring about NaN (Not a Number):

1 |
var x = 100 / "Apple"; // x will be NaN (Not a Number) |

Be that as it may, if the string contains a numeric worth , the outcome will be a number:

1 |
var x = 100 / "10"; // x will be 10 |

Be that as it may, if the string contains a numeric worth , the outcome will be a number:

1 2 |
var x = 100 / "Apple"; isNaN(x); // returns true because x is Not a Number |

Watch out for NaN. On the off chance that you use NaN in a scientific activity, the outcome will likewise be NaN:

1 2 3 |
var x = NaN; var y = 5; var z = x + y; // z will be NaN |

Or on the other hand the outcome may be a connection:

1 2 3 |
var x = NaN; var y = "5"; var z = x + y; // z will be NaN5 |

NaN is a number: typeof NaN returns number:

1 |
typeof NaN; // returns "number" |

## Interminability

Interminability (or – Infinity) is the worth JavaScript will return on the off chance that you ascertain a number outside the biggest conceivable number.

1 2 3 4 |
var myNumber = 2; while (myNumber != Infinity) { // Execute until Infinity myNumber = myNumber * myNumber; } |

Division by 0 (zero) additionally creates Infinity:

1 2 |
var x = 2 / ; // x will be Infinity var y = -2 / ; // y will be -Infinity |

Boundlessness is a number: typeof Infinity returns number.

1 |
typeof Infinity; // returns "number" |

## Hexadecimal

JavaScript deciphers numeric constants as hexadecimal in the event that they are gone before by 0x.

1 |
var x = 0xFF; // x will be 255 |

Never compose a number with a main zero (like 07).

Some JavaScript variants decipher numbers as octal on the off chance that they are composed with a main zero.

As a matter of course, JavaScript shows numbers as base 10 decimals.

Be that as it may, you can utilize the toString() strategy to yield numbers from base 2 to base 36.

Hexadecimal is base 16. Decimal is base 10. Octal is base 8. Twofold is base 2..

1 2 3 4 5 6 |
var myNumber = 32; myNumber.toString(10); // returns 32 myNumber.toString(32); // returns 10 myNumber.toString(16); // returns 20 myNumber.toString(8); // returns 40 myNumber.toString(2); // returns 100000 |

## Numbers Can be Objects

Regularly JavaScript numbers are crude qualities made from literals:

var x = 123;

Be that as it may, numbers can likewise be characterized as articles with the catchphrase new:

var y = new Number(123);

1 2 3 4 5 |
var x = 123; var y = new Number(123); // typeof x returns number // typeof y returns object |

Try not to make Number items. It hinders execution speed.

The new catchphrase convolutes the code. This can create some surprising outcomes:

When utilizing the == administrator, equivalent numbers are equivalent:

1 2 3 4 |
var x = 500; var y = new Number(500); // (x == y) is true because x and y have equal values |

When utilizing the === administrator, equivalent numbers are not rise to, on the grounds that the === administrator anticipates uniformity in both sort and worth.

1 2 3 4 |
var x = 500; var y = new Number(500); // (x === y) is false because x and y have different types |

Or on the other hand far more atrocious. Articles can’t be thought about:

1 2 3 4 |
var x = new Number(500); var y = new Number(500); // (x == y) is false because objects cannot be compared |