8/21/2023 0 Comments Javascript interview questionsThe 3 scheduled log() callbacks are called by setTimeout().It's important to understand that the closure captures i variable lexically. Log() is a closure that captures the variable i, which is defined in the outside scope of for() cycle. When for() cycle completes, i variable has value 3.Then setTimout() schedules an execution of log(). During each iteration a new function log() is created, which captures the variable i. There are 2 phases behind executing this snippet. When I first tried to solve it, this was my answer too! If you haven't heard about this tricky question before, most likely your answer is 0, 1 and 2: which is incorrect.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |