DEV Community

Mehul Lakhanpal
Mehul Lakhanpal

Posted on

The World famous javascript interview question

Alt Text

Write your answers below.

Knowing the output is not that important, understanding why the output is important!

Thanks for reading 💙

Follow @codedrops.tech for daily posts.

InstagramFacebook

Micro Learning ● Web Development ● Javascript ● MERN stack

codedrops.tech

Top comments (3)

Collapse
 
chiefgui profile image
Guilherme Oderdenge

For each tick of your loop (5 ticks in total), it'll schedule console.log to be run with the value of i after 1000ms.

Meaning that in approximately ~1200ms (1000ms of the schedule plus some ms of the execution operation) after the execution of this code, the console will display 0, 1, 2, 3, 4.

Am I correct?

Collapse
 
ml318097 profile image
Mehul Lakhanpal

Good explanation but its wrong. var is scoped functionally and also because of closures, the value of i would be 5 when the cb is executed. Hence, it will print out

5
5
5
5
5
Collapse
 
chiefgui profile image
Guilherme Oderdenge

Ha! You got me! Thanks for clarifying!