Search This Blog

2023/06/21

Coding Interview Question : Output of Code snippet

What will be output of following code:

Example 1:

const b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

for (let i = 0; i < 10; i++) {
setTimeout(() => console.log(b[i]), 1000);
}

for (var i = 0; i < 10; i++) {
setTimeout(() => console.log(b[i]), 1000);
}


output:
1
2
3
4
5
6
7
8
9
10
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined

No comments:

Post a Comment