If I run
for (let i = 0; i < 1000; i++) {
console.log(i)
}
window.alert('Hello World!');
then alert box pops up before for loop gets done.
But instead of using for loop, if I write simply console.log(1).. console.log(1000) statements and then put window.alert('Hello World'). It waits until all the console.log function gets executed.
console.log(1)
console.log(2)
.
.
.
console.log(1000)
console.log('Hello World');
Both ways of putting console.log statements do the same thing. But behaviour of alert box is different in each situation.
Why it happens so ? If alert is part of web API, shouldn't it get pushed inside the message queue and then pushed to the call stack only (by event loop) when stack becomes empty ? Why alert appears in middle of for loop in first case ?