Two Phases: Compilation and Execution
Execution: Following successful compilation, the engine proceeds to execute the code line by line, managing variables, scope, and memory during the process. For each function call, a new Execution context is created, and both the phases are executed again for that function.
The Global Object
There are 2 types of Execution Contexts
Function Execution Context: Every function call spawns a new execution context, containing the function's code and its local variables.
The Call Stack
The call stack, a fundamental data structure, keeps track of execution contexts. When a function is called, a new execution context is pushed onto the stack, and when the function returns, the context is popped off the stack. It follows LIFO (Last In First Out) technique.
Variables and Scope
Global Scope: Variables declared outside of functions reside in the global scope, making them accessible from any part of your code.
Function Scope: Variables declared within a function using var are function-scoped, limiting their accessibility to that function. But due to Hoisting, they can still be accessible in global scope.
Block Scope: Variables declared within blocks using let and const are block-scoped, only accessible within that block.
Closures and Lexical Scope
Minification: Reduce the size of your code by eliminating whitespace and shortening variable names.
Caching: Store frequently used data or results to minimize redundant computations.
Event Delegation: Reduce the number of event listeners through event delegation.