setTimeout in for loop using 'var' keyword interview question

Опубликовано: 19 Май 2026
на канале: Code Chacha
61
4

setTimeout in for loop using 'var' keyword, hoisting, javascript

When using setTimeout inside a for loop with var, you might encounter unexpected behavior due to variable hoisting and scoping in JavaScript. Here's a quick guide on how this works and how to fix it.

Problem Overview
Using var inside a for loop with setTimeout can lead to all timers referencing the same variable value due to hoisting.

how to use setTimeout javascript
hoisting example

#setTimeout #javascript #hoisting #varKeyword #interview #interviewquestions #mostaskedquestion #shorts #short #coding #javascript #code-chacha #codechacha