awesome-everything RU
↑ Back to the climb

Base CS from zero

Values and types: code reading

Crux Read four short JS snippets and predict what the runtime actually does: float rounding, the safe-integer limit, silent coercion, and the typeof null quirk.
Your altitude — climbing toward senior
ZeroJuniorMiddleSenior
You are at middle altitude — in the sky
◷ 14 min

The surprises in everyday code all trace back to the unit’s core idea: bits are stored under one type rule and later read under another. Read each snippet, predict the exact output, and name the rule that produced it.

Goal

Practise the loop you run whenever a program prints something unexpected: identify which type rule the runtime applied to the bits, and explain why the output is what it is rather than what you first guessed.

Snippet 1 — the fraction that will not add up

const sum = 0.1 + 0.2;
console.log(sum);            // ?
console.log(sum === 0.3);    // ?
Quiz

What does this print, and why?

Snippet 2 — the integer that stops counting

const big = 9007199254740991;   // Number.MAX_SAFE_INTEGER
console.log(big + 1);           // ?
console.log(big + 1 === big + 2); // ?
Quiz

What does this print, and what is the underlying cause?

Snippet 3 — the plus sign that changes its mind

console.log(5 + "5");   // ?
console.log(5 - "5");   // ?
console.log("5" * 2);   // ?
Quiz

What three lines print, and what rule explains the difference?

Snippet 4 — the type that lies

console.log(typeof null);        // ?
console.log(typeof undefined);   // ?
console.log(null === undefined); // ?
Quiz

What does each line print, and which result is a historical quirk?

Recap

Every surprise here is the same unit idea in a JS costume. Float rounding (0.1 + 0.2) and the safe-integer limit (above 2^53) both come from the single 64-bit IEEE 754 double having finite precision — there is no separate integer type to fall back on. The + operator concatenates with strings but coerces to numbers for - and *, so dynamic typing silently picks a type rule for you. And typeof null returns ‘object’ — a frozen historical bug. Read the operator and the storage format, and the output stops being a mystery.

Continue the climb ↑Values and types: build a value inspector
shortcuts expand
search
K
prev piece
k
next piece
j
cycle tier
t
this menu
?
sources3
expand
  1. 01
  2. 02
  3. 03

Trademarks belong to their respective owners. Editorial reference only.