Script Valley
Debugging: A Systematic Approach
Reading Code to Find BugsLesson 2.3

How to spot common JavaScript and Python bug patterns

type coercion bugs, mutable default arguments, closure variable capture, async callback order, reference vs value

Language-Specific Bug Patterns

Every language has recurring bug patterns that experienced developers recognize on sight. Learning these patterns lets you skip the hypothesis step for common bugs -- you see the code shape and know the failure mode immediately.

JavaScript: Type Coercion and Closures

// Coercion
console.log([] + []);   // empty string
console.log([] + {});  // [object Object]

// Closure trap in loops
for (var i = 0; i < 3; i++) {
  setTimeout(() => console.log(i), 0); // prints 3,3,3 not 0,1,2
  // Fix: use let instead of var
}

Python: Mutable Defaults and Late Binding

# Mutable default argument -- shared across all calls
def add_item(item, lst=[]):   # lst created ONCE at definition
    lst.append(item)
    return lst

add_item(1)  # [1]
add_item(2)  # [1, 2] -- not [2], same list reused
# Fix: use lst=None and create inside function

# Late binding in lambdas
fns = [lambda: i for i in range(3)]
print(fns[0]())  # 2, not 0 -- all lambdas capture same i

Build a personal cheat sheet of these patterns. When a bug matches a pattern, you have both the diagnosis and the fix in seconds.

Up next

How to do a code review to find bugs before running code

Sign in to track progress