Education logo

Understanding Execution Context and Execution Stack in JavaScript: The Core Concepts for Efficient and Error-Free Code

Understanding execution context and execution stack is crucial for writing efficient and error-free JavaScript code

By FARDA KARIMOVPublished 3 years ago 4 min read

The popular programming language JavaScript is frequently used to create web apps. One of the core concepts of JavaScript is the concept of execution context and execution stack. Understanding these concepts is essential for writing efficient and error-free JavaScript code. In this article, we will dive into the details of execution context and execution stack in JavaScript with code examples.

What is an Execution Context?

An execution context is an environment in which JavaScript code is executed. It includes all the necessary variables, functions, and objects that are needed to execute the code. Each execution context has its own set of variables, functions, and objects that are unique to that context.

The JavaScript engine creates a new execution context every time it executes a function. Each execution context is created in two phases: the creation phase and the execution phase. During the creation phase, the JavaScript engine creates the scope chain, the variable object, and this object. During the execution phase, the JavaScript engine executes the code in the context.

The Scope Chain

The scope chain is a list of all the variable objects that are in scope for a particular execution context. When JavaScript code is executed, the JavaScript engine looks for a variable in the current execution context. If the variable is not found, it searches for the next variable object in the scope chain until the variable is found or until the global scope is reached.

Consider the following code example:

In this example, the execution context for the foo() function is created when it is called. The scope chain for this execution context includes the variable object for the foo() function and the global variable object. When the console.log() statement is executed, the JavaScript engine first looks for the y variable in the variable object for the foo() function. Since it is found, the value of y is added to the value of x (which is in the global scope) to produce the output of 30.

The Variable Object

The variable object is a container that holds all the variables and function declarations that are in scope for a particular execution context. It is created during the creation phase of an execution context and is used during the execution phase to resolve variable references.

Consider the following code example:

In this example, the execution context for the foo() function is created when it is called. During the creation phase of the execution context, the JavaScript engine creates a variable object for the function. This variable object contains a reference to the function itself (foo) and a reference to the variable x. During the execution phase, the console.log() statement references the x variable, which is found in the variable object for the foo() function, and its value of 10 is outputted

The this Object

The this object refers to the object that the current function is a method of. It is determined by how the function is called. If the function is called as a method of an object, this refers to that object. If the function is called without an object reference, this refers to the global object.

Consider the following code example:

In this example, the person object has a sayHi() method that logs a message to the console. When the sayHi() method is called on the person object, this keyword inside the method refers to the person object, so the message that is outputted includes the name property of the person object.

The Execution Stack

The execution stack is a stack data structure that is used to keep track of the current execution context. When a function is called, a new execution context is created and pushed onto the top of the execution stack. When the function completes execution, its execution context is popped off the top of the execution stack, and control is returned to the calling function.

Consider the following code example:

In this example, when the bar() function is called, a new execution context is created for it and pushed onto the execution stack. Inside the bar() function, the foo() function is called, which creates a new execution context for itself and pushes it onto the top of the execution stack. When the foo() function completes execution, its execution context is popped off the top of the execution stack, and control is returned to the bar() function, which continues execution from where it left off.

Conclusion

In summary, execution context and execution stack are important concepts in JavaScript that are essential for understanding how JavaScript code is executed. An execution context is an environment in which JavaScript code is executed, and it includes all the necessary variables, functions, and objects that are needed to execute the code. The scope chain, variable object, and this object are all parts of the execution context. The execution stack is a stack data structure that is used to keep track of the current execution context. Understanding these concepts will help you write efficient and error-free JavaScript code.

how tointerview

About the Creator

FARDA KARIMOV

I'm a front-end engineer creating visually appealing and user-friendly web experiences. On Vocal Media, I share insights and advice on front-end development and design trends. Join me to explore the world of front-end development.

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2026 Creatd, Inc. All Rights Reserved.