Facebook Pixel

Does an IIFE capture the value or the variable in JavaScript?

It captures a new parameter (j), which is a copy of the value (for primitives). The closure inside the IIFE closes over j, not the outer i. So even if i changes later, j holds the value at the time of the IIFE call.

Verify This Answer

Cross-check this information using these trusted sources:

More FAQs in Closures Capture the Variable, Not the Value in JavaScript

The variable. A closure holds a reference to the variable, not a snapshot of its value. If the variable changes before the callback runs, the closure sees the updated value. This is why var in a loop logs the final value.

Because they all close over the same variable i. Closures capture the variable, not the value. When the callbacks run, i is the final value, so all callbacks log it.

Use let (creates a fresh binding per iteration), or an IIFE that passes the current value as a parameter (pass-by-value for primitives), or pass the value as an extra argument to setTimeout.

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.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.