Execution Context & Call Stack
How JavaScript Code Executes
JavaScript uses a single-threaded model with an event loop. It executes code in two phases:
Creation Phase: Memory allocation for variables and functions.
Execution Phase: Runs the code line by line.
Example:
jsCopyEditconsole.log("Start");
function greet() {
console.log("Hello!");
}
greet();
console.log("End");
โ Execution Order:
"Start"prints.greet()is called, and"Hello!"prints."End"prints.
๐ Call Stack Visualization:
console.log("Start")โ Pushed & executed.greet()โ Pushed, executesconsole.log("Hello!"), then popped.console.log("End")โ Pushed & executed.