Why does this print 5 five times? 🤯 JavaScript Scope Trap! || JavaScript Interview Question #shorts

preview_player
Показать описание
In this short video, I’ll test your JavaScript skills with a tricky question about closures! Can you solve it? Let me know in the comments. Don’t forget to like and subscribe for more #JavaScript and #Python coding challenges!
#shorts #javascript #python #codingchallenge #programming
Рекомендации по теме
Комментарии
Автор

Answer - 5, 5, 5, 5, 5
The setTimeout function is called inside of a loop that iterates through the elements in the arr array. The setTimeout function will execute its callback function after a delay of 1000 milliseconds. However, by the time the delay has elapsed and the callback function is called, the loop will have already completed and the value of i will be 5. As a result, the output will be 5 printed five times.

syntax_hub
Автор

Did you get it right? Tell me below! 😄

syntax_hub
Автор

Its B

as the loop itself is not waiting for 1s so It will set I to 5 and then the console.log(i) prints the i = 5

😊

eternityzero-king
visit shbcf.ru