Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 1.3 — Numbers in Depth
JavaScript · Example 1 of 4

Decimals, rounding errors and safe comparisons

Numeric separators (1_000_000) arrived in ES2021.

Every JavaScript number is a 64-bit floating-point value, which is why 0.1 + 0.2 isn't exactly 0.3, why NaN and Infinity exist, and why whole numbers above 2^53 − 1 need BigInt.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Add the prices 19.99 and 5.01 so that the result is exactly 25 and log true when you compare it with 25.

02

Log whether Number("12px") is NaN using the correct test, and log the result of 100 / 0.

03

Log 2 to the power of 70 exactly.

Explain it without notes

01

Why does 0.1 + 0.2 not equal 0.3 in JavaScript, and how do you handle money?

02

What is NaN, why is NaN === NaN false, and how do you test for it?

03

What is Number.MAX_SAFE_INTEGER and what happens beyond it?

04

When would you use BigInt, and what are its limits?

Decimals, rounding errors and safe comparisons

Console

(nothing was printed)

Expected console output

3.5
0.30000000000000004
false
true
20
1000000 1000 255 5
true