Understanding Closure: Detailed Exercises Explained

Nishit RanjanNishit Ranjan
1 min read

Q1:

In this above questions output will be - 0,1,2

Q2

so in this output will be 3,3,3
because var is a function scope so only one i shared by all iterations.
so in case of let i is created in all the iteration
{
let i = 0;
setTimeout(() => console.log(i), 0);
},
{
let i = 1;
setTimeout(() => console.log(i), 0);
},
{
let i = 2;
setTimeout(() => console.log(i), 0);
}

Q3

O/p will be -JS, Closures

0
Subscribe to my newsletter

Read articles from Nishit Ranjan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nishit Ranjan
Nishit Ranjan

Self-taught frontend developer on a mission to master JavaScript and web development. Sharing my journey, tips, and hard-learned lessons through blogs and projects.