</> Code Corner

JavaScript · JS Tutorial · Lesson 17

JS Numbers

One number type, NaN, Infinity and precision.

JavaScript has only one type of number. Numbers can be written with or without decimals, and are stored as 64-bit floating point.

Example

<p id="demo"></p>
<script>
let out = "";
out += (0.1 + 0.2) + "<br>";
out += ((0.1 * 10 + 0.2 * 10) / 10) + "<br>";
out += (100 / "Apple") + "<br>";
out += (2 / 0) + "<br>";
out += ("10" + 20) + "<br>";
out += ("10" * 20) + "<br>";
out += Number.isInteger(5.0);
document.getElementById("demo").innerHTML = out;
</script>
  • NaN ("Not a Number") results from arithmetic with a non-numeric string
  • Infinity results from numbers too large or dividing by zero
  • Adding a number and a string concatenates; other operators convert strings to numbers
  • Floating point arithmetic is not always 100% accurate

Keep reading “JS Numbers”

Sign up to read the full lesson, try every example in the live editor, and take the 5-question quiz.

Chat with us 👋