Search⌘ K

Understanding Scopes

Explore how JavaScript manages execution contexts and scope chains to control variable accessibility and function behavior. Understand the global and function contexts, the role of the variable object, and how identifiers are resolved during code execution. This lesson helps you grasp key concepts like function hoisting and nested function scopes to improve your coding skills.

JavaScript has an important concept, execution context, which should be understood by all developers.


The context of a function or variable defines how the function or variable behaves, as well as other data it has access to.


Behind the scenes, the JavaScript engine associates a variable object to each execution context, and all functions and variables defined in the context exist in this variable object.

📜NOTE: The variable object is not accessible by code.

When a function is invoked, a new context is created for the function and it is pushed onto a context stack. After the function has been executed, this stack is popped, and the control is returned to the previously executed context.

There is a global execution context, it is the outermost one, the first to be put onto the context stack. In web browsers, the global context belongs to the window object which represents an instance of the browser.

When you create ...