Classic JS Gotchas

Survive the technical interview. Master the famous JavaScript quirks surrounding Type Coercion, asynchronous loops, and the 'this' keyword.

JavaScript9 min readConcept 63 of 63

The JavaScript Quirks

JavaScript was created in just 10 days in 1995. To make it forgiving for beginners, its creator added aggressive Type Coercion and relaxed scoping rules. Decades later, these features have become famous 'gotchas' that interviewers love to test.

A 'Gotcha' is a piece of code where the obvious, intuitive answer is completely wrong because of a hidden engine mechanic.

Why interviewers care

Interviewers don't ask these questions to trick you; they ask them to see if you understand the underlying engine. If you know *why* 0 == false but 0 !== false, it proves you understand Type Coercion versus Strict Equality.

Memorizing the answers isn't enough. You must understand the mechanics.

The Three Classic Traps

**1. The typeof Trap:** javascript typeof null; // 'object' (A famous bug from 1995) typeof NaN; // 'number' (Not-A-Number is technically a numeric type)

**2. The var Loop Trap:** javascript for (var i = 0; i < 3; i++) { setTimeout(() => console.log(i), 100); } // Logs: 3, 3, 3. Because 'var' is globally scoped, the loop finishes before the timeouts run, leaving 'i' at 3.

**3. The Context Trap:** javascript const dog = { sound: 'woof', bark() { return this.sound; } }; const looseBark = dog.bark; looseBark(); // undefined. The function lost its object context!

The Hot Seat

Welcome to your technical interview. Pick a famous trap below, make your prediction, and then run the engine evaluation to see how JavaScript actually interprets the code.

The Engine Decoder

The Code
typeof null;
1
Initial Call
The engine receives the typeof operator for the value null.
2
Binary Inspection
In 1995 JavaScript, values were stored in 32-bit units. The first 3 bits indicated the type tag. Objects had a type tag of 000.
3
The Null Pointer
The null value was represented as the machine code NULL pointer (all zeros: 00000000).
4
The Famous Bug
Because the null pointer started with 000, the engine's type-checker accidentally classified it as an Object!
5
Result
Returns 'object'. Fixing it now would break the web, so the bug remains forever.
iWhen facing a "Gotcha" in an interview, do not just blurt out the answer. Talk through the engine's internal steps out loud. That is what the interviewer actually wants to hear.

Check yourself

Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.

  1. 1What is the output of `typeof null`?
  2. 2Why does `for (var i=0; i<3; i++) { setTimeout(()=>console.log(i), 0); }` log `3, 3, 3`?
  3. 3How do you extract a method from an object without losing its `this` context?

Remember this

  • typeof null is 'object' due to a historical bug.
  • typeof NaN is 'number' (Not-A-Number is a numeric data type).
  • Always use strict equality (===) to avoid bizarre Type Coercion bugs.
  • Loops with var and setTimeout will share a single variable. Use let to bind a new variable per iteration.
  • Extracting a function from an object strips its this context. Use .bind() to lock it down.

Done with this concept?

Mark it complete to track your progress. No login needed.