Frontend Development: Execution Context

Peter Jeon·2023년 6월 27일
0

Frontend Development

목록 보기
32/80
post-custom-banner

Cover Image

Introduction to Execution Context

![JavaScript Execution Context](https://source.unsplash.com/1600x900/?JavaScript,Execution Context)

Execution context is a fundamental concept in JavaScript, and understanding it can help you write more effective code. At its simplest, an execution context is an environment in which JavaScript code is evaluated and executed.

When the JavaScript engine runs your code, it creates global execution contexts and function execution contexts. These execution contexts have two phases: creation and execution.

Creation Phase

Creation Phase

During the creation phase, the JavaScript engine creates a variable object, a scope chain, and a this variable.

Consider the following code snippet:

var a = 'Hello World';

function first() {
  var b = 'Hello you';
  second();
  
  function second() {
    var c = 'Hello there';
    console.log(a + b + c);
  }
}

first();

Execution Phase

During the execution phase, the JavaScript engine starts executing the code line by line. In the above code snippet, when we invoke the first() function, a new execution context is created and placed on top of the execution stack.

Execution Stack

The execution stack, also known as the call stack, handles the execution of multiple contexts. When your code calls a function, a new context for that function is created and added to the stack.

Conclusion

Understanding execution context is key to mastering JavaScript. It helps to explain how variables or functions are accessed during code execution, even before they have been declared in the code. Keep in mind the creation and execution phases of the context, as well as the role of the execution stack in managing multiple contexts.

profile
As a growing developer, I am continually expanding my skillset and knowledge, embracing new challenges and technologies
post-custom-banner

0개의 댓글