← All posts

Seven JavaScript puzzles that expose your mental model

November 24, 2026

Here is a rule for learning to code: guess first, then check. Reading an explanation feels like understanding. Committing to an answer, being wrong, and finding out why is what actually builds a mental model.

So here are seven small puzzles. Cover the answers, say what each line evaluates to, then open the answer. Don’t run them yet. The point is the guess.

1. The sort that isn’t

const nums = [10, 9, 1];
nums.sort();
nums
Answer

[1, 10, 9]. With no comparison function, sort() turns items into strings and compares those. "10" sorts before "9" because "1" is less than "9". For numbers, write nums.sort((a, b) => a - b).

2. Money math

0.1 + 0.2 === 0.3
Answer

false. Numbers are stored in binary, and 0.1 and 0.2 have no exact binary form. The sum is 0.30000000000000004. This isn’t a JavaScript quirk: any language using standard floating point does the same. For money, count whole cents.

3. The map that breaks

["1", "2", "3"].map(parseInt)
Answer

[1, NaN, NaN]. map calls its function with (item, index), and parseInt takes (text, radix). So the calls are parseInt("1", 0), parseInt("2", 1) and parseInt("3", 2). The last two ask for a base that can’t hold those digits.

4. Closures in a loop

const fns = [];
for (var i = 0; i < 3; i++) fns.push(() => i);
fns.map((f) => f())
Answer

[3, 3, 3]. var has one i for the whole function, and by the time the functions run the loop has finished with i at 3. Change var to let and each pass gets its own i, so you get [0, 1, 2].

5. The NaN that finds itself

[NaN].includes(NaN)
Answer

true, even though NaN === NaN is false. includes uses a slightly different comparison that treats NaN as equal to itself. The older [NaN].indexOf(NaN) uses === and returns -1. Same idea, two different equalities.

6. Loose equality and null

[null == undefined, null == 0, null >= 0]
Answer

[true, false, true]. == has a special rule that makes null and undefined equal only to each other, so null == 0 is false. But >= converts null to the number 0 first, so null >= 0 is true. Two operators, two sets of rules. This is why many teams ban ==.

7. The famous banana

"b" + "a" + +"a" + "a"
Answer

"baNaNa". The +"a" in the middle is a unary plus, which converts "a" to a number. That fails and gives NaN. Then string concatenation turns it back into text: "ba" + NaN + "a".

What your score really tells you

Missing a puzzle is not a failure. Every wrong guess points at an exact spot where your picture of the language differs from the real one. Those are the places to fix.

If you want hundreds of exercises built this way, with real code running as you answer, that’s what Paren is.

Keep predicting

Paren's JavaScript Essentials course teaches the whole language this way. Free to start.

Start free