Facebook Pixel

How does setTimeout's extra arguments fix the closure bug in JavaScript?

setTimeout(fn, delay, ...args) passes the extra arguments to the callback. Each call captures the current value of i as an argument. The callback receives j (a copy), not the shared i.

Verify This Answer

Cross-check this information using these trusted sources:

More FAQs in Fixing the setTimeout Closure Bug Three Ways in JavaScript

Use let (fresh binding per iteration), use an IIFE (new function scope with pass-by-value parameter), or use setTimeout's extra arguments (pass the current value as an argument to the callback).

let. It is the cleanest and most readable. It creates a fresh binding per iteration without extra nesting or arguments. Use it in all ES6+ code.

The IIFE creates a new function scope per iteration and passes i as a parameter (j). Primitives are passed by value, so j is a copy of i at that iteration. The closure captures j, not i.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.