JavaScript Tutorial

JavaScript Numbers

JavaScript has one number type for integers and floats. NaN and Infinity are values, not crashes.

One type for all ordinary numbers

Python splits integers and floats. JavaScript does not. 42 and 3.14 are bothtypeof "number". There is no separate integer type for everyday counting and money.

Write whole numbers without a decimal. Write fractions with a dot. Scientific notation such as1.5e3 is still a number: it means 1500.

Run every example in /javascript/try. console.log lands in the console under the page.

Integers and floats

Both of these values have the same type. Arithmetic works on either. Dividing two whole numbers can produce a fraction: 5 / 2 is 2.5, not 2.

Example

const whole = 42;
const fraction = 3.14;
const scientific = 2.5e2;

console.log(typeof whole);
console.log(typeof fraction);
console.log(scientific);
console.log(5 / 2);

document.body.textContent =
  typeof whole + " " + typeof fraction + " " + scientific + " " + (5 / 2);

NaN is a value

NaN means “not a number.” It is still typed as "number". You get it from math that does not make sense: Number("hello"), 0 / 0, or Math.sqrt(-1).

The program does not stop. Later math with NaN stays NaN. That is why a total can become blank or “NaN” on the page if an input was not numeric.

Example

const bad = Number("hello");
console.log(bad);
console.log(typeof bad);
console.log(0 / 0);
console.log(bad + 10);

document.body.textContent = String(bad) + " " + typeof bad + " " + (bad + 10);

NaN === NaN is false. Use Number.isNaN(value) to test. The olderisNaN(value) coerces first, so isNaN("hello") is true for the wrong reason.

Infinity is a value

Divide a positive number by 0 and you get Infinity. Divide a negative number by0 and you get -Infinity. Again, this is not a crash. It is a number you can store, print, and compare.

Example

console.log(1 / 0);
console.log(-1 / 0);
console.log(Number.POSITIVE_INFINITY);
console.log(1 / 0 === Infinity);

document.body.textContent = String(1 / 0) + " " + String(-1 / 0);

Precision has a limit

JavaScript numbers are 64-bit floating point. Whole numbers are exact up toNumber.MAX_SAFE_INTEGER (9007199254740991). Past that, some integers cannot be represented and look equal when they are not.

Tiny fractions also surprise you: 0.1 + 0.2 is not exactly 0.3. For money, store cents as integers, or round with the methods in the next chapter.

Example

console.log(0.1 + 0.2);
console.log(0.1 + 0.2 === 0.3);
console.log(Number.MAX_SAFE_INTEGER);
console.log(9007199254740993 === 9007199254740992);

document.body.textContent = String(0.1 + 0.2);

Special number values

ValueHow you get ittypeof
42A literal integer"number"
3.14A literal float"number"
NaNFailed numeric work"number"
InfinityOverflow or divide by zero"number"

bigint exists for huge integers, written with an n suffix such as10n. You do not need it for this track. Mix a bigint with a number and JavaScript throws.

What to remember

  • One type covers integers and floats.
  • NaN and Infinity are values. The script keeps running.
  • Test with Number.isNaN, not === NaN.
  • Do not trust tiny decimal sums for money without rounding.

Next: methods that turn numbers into text and text back into numbers.

FAQ: JavaScript Numbers

Common questions about this page.

What is the StudyGrid JavaScript tutorial?

The StudyGrid JavaScript tutorial is a full beginner track: variables, functions, arrays, if/else, the DOM, events, storage, and fetch. Each chapter has copy-and-run examples.

Should I run javascript numbers examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn javascript numbers in this JavaScript JavaScript lesson (JavaScript Numbers).

Is the JavaScript editor the same as Try Python?

No. Try JavaScript is a live page plus a console at /javascript/try. Try Python stays at /try. JavaScript lessons never open the Python editor.

Do I need to install anything to learn JavaScript?

No. Open a chapter, click Try it in JavaScript, and the page and console update in the browser.

Where should I start the JavaScript tutorial?

Start at JavaScript Intro, then Where To, Output, and Syntax. After variables and functions, continue to the DOM and events. Use Next at the bottom of each chapter.

Is the JavaScript tutorial free?

Yes. The JavaScript studio on StudyGrid (studygrid.in) is free: dashboard, chapters, and the live editor.