I have the following code:
function sayHiLater(){
var greeting = "Hi!";
setTimeout(function() {
console.log(greeting);
}, 3000);
console.log("Hello before hi");
}
sayHiLater();
I would like to better understand how event listeners work under the hood, so in my example above, as setTimeOut is being executed, what really happens? I know it creates a new execution context, but my question is; does that execution context is simply being delayed for 3 seconds, meaning the execution stack is moving on to other things in the meanwhile, and as the 3 seconds are over it comes back to that execution context, or is it simply passing to the browser engine some sort of a reference of the anonymous function argument, telling it when to fire, and then right away the setTimeOut execution context is being popped off the execution stack. Or am I just completely far off from what's really happening. Thank you for your time.