JavaScript Numbers

There is no separate data type in JavaScript for float, decimal, long or integer numbers.

A number can contain decimal point or can be without decimal point.

JavaScript Numbers

JavaScript numbers can contain decimal point or it can be without decimal point

If the argument cannot be converted into a number, it returns NaN.

var temperature = 45.5;     // Written with decimals
var int = 95;               // Written without decimals

It is also possible to use scientific notation to write large numbers.

var y = 999e5;      // 99900000
var z = 999e-5;     // 0.00999

JavaScript Numbers as Double

Internally, JavaScript numbers are double values i.e. 64-bit based on the IEEE Standard for Floating-Point Arithmetic (IEEE 754)

As per this format, numbers stores in 64 bits, where the number part is stored in 0 to 51 bits,
the exponent part stored in bits 52 to 62, and the sign part stored in bit 63:

Precision

A number without a decimal point or exponent are consider as accurate up to 15 digits.

<!DOCTYPE html>
<html>
<body>

<p>Integers are accurate up to 15 digits only. After that it returns false value.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
var num1 = 99999999999999999;
var num2 = 999999999999999;
document.getElementById("demo").innerHTML = num1 + "<br>" + num2;
}
</script>

</body>
</html>

The maximum number of decimals allowed for float number is 17, even though floating point arithmetic is not always 100% accurate but due to 17 decimals, change in the accuracy is negligible:

<!DOCTYPE html>
<html>
<body>

<p>Float numbers are not 100% accurate always.</p>

<p id="demo"></p>

<script>
var floatnum = 0.2+0.4;  // float will be 0.6000000000000001
document.getElementById("demo").innerHTML = "0.2 + 0.4 = " + floatnum;
</script>

</body>
</html>

However sometimes it returns proper value

<!DOCTYPE html>
<html>
<body>

<p>However sometimes Float numbers are 100% accurate.</p>

<p id="demo"></p>

<script>
var floatnum = 0.1+0.1;  // float will be 0.2
document.getElementById("demo").innerHTML = "0.1 + 0.1 = " + floatnum;
</script>

</body>
</html>

In JavaScript, if a number starts with 0x, it is interpreted as hexadecimal number.

<!DOCTYPE html>
<html>
<body>

<p>Numbers starts with 0x interprets as Hexadecimal.</p>

<p id="demo"></p>

<script>
var hexnum = 0xAF;             // x will be 175
document.getElementById("demo").innerHTML = "Value of 0xAF = " + 0xAF;
</script>

</body>
</html>

Do not prefix a number with 0 like 050, as some versions of JavaScript interpret it as an octal number.

Infinity

If the calculated number in JavaScript exceeds largest possible number or divided by zero than its value set to Infinity for plus numbers and -Infinity for minus numbers.

<!DOCTYPE html>
<html>
<body>

<p>If the calculated number in JavaScript exceeds largest possible number than its value set to Infinity</p>

<p id="demo"></p>

<script>
var number = 3;
var str = number + '<br />';
while (number != Infinity) {          // Execute until Infinity
number = number * number * number;
str = str + number + '<br />';
}

document.getElementById('demo').innerHTML = str;
</script>

</body>
</html>

typeof Infinity returns a number as it is a out of range number value

<!DOCTYPE html>
<html>
<body>

<p>typeof Infinity is </p>

<p id="demo"></p>

<script>
document.getElementById('demo').innerHTML = typeof Infinity;
</script>

</body>
</html>

NaN

NaN is a JavaScript reserved word for notify value is not a number

Any arithmetic operation with NaN results in a NaN value

var num = 100 / "String";

If a string contains numeric value, it is converted to number and operation will result in a number value

<!DOCTYPE html>
<html>
<body>

<p>Division Operation with NaN</p>

<p id="demo"></p>

<p>If String contains numeric value, it is converted to number first</p>
<p id="demo1"></p>

<script>
document.getElementById('demo').innerHTML = 100 / "String";
document.getElementById('demo1').innerHTML = 100 / "20";
</script>

</body>
</html>