DEV Community

Sumeet Shedge
Sumeet Shedge

Posted on

Execution Context and Hoisting

In JavaScript, execution context is one of the important topic. With understanding of execution context, you can become better JavaScript developer. let's dive into execution context.

What is Execution Context?

  1. Execution context is like wrapper which helps to running our code better. It's created on function invocation with lots of things.

  2. Execution context wraps our code as well it contain multiple things which is created by JavaScript Engine.

  3. browser created following things for us -

    • window object,
    • this variable,
    • Reference to outer Variable Enviornment,
    • Lexical Enviornment.
  4. Execution context is created within 2 phases - creation phase and code execution phase.

  5. creation phase - in creation phase, we got global object(only for Global execution context), this, lexical Enviornment and Reference to outer Enviornment (except Global Execution context) from JavaScript engine. And JavaScript engine setup memory space for variable and function in creation phase. this setting up space phenomenon called Hoisting.

  6. code execution phase -
    In this phase, JavaScript engine actually run code and execute operations.

b();
console.log(a);

var a = "Hello World";
function b(){
  console.log("b is called");
}
Enter fullscreen mode Exit fullscreen mode

in console window-
b is called
undefined

Hoisting

In JavaScript, when browser run your code and first it create memory space for variable and function in creation phase. when it runs your code, so here they first execute function and then variable.

function is get called properly but variable shows undefined value. because, when browser sets memory for variable, it store with undefined value.

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay