In my interview I was asked what will be the output when this javascript code runs.
setTimeout(function() {
console.log(`Set time out`);
}, 0)
while (true) {
console.log("While")
}
I knew that setTimeout adds the callback after the time provided in the argument.
So I thought as queue is initially empty then setTimeout callback will be added then then while loop will be added. So first timeout will run as the wait time is 0ms, then infinite loop will run.
But the interviewer said first while loop will start and it will never end so setTimeout will never execute.
But I tried googeling to find why while is executed before setTimeout even thought it is after the setTimeout but didn’t found anything appropriate.
Can anyone provide me with explanation