My FeedDiscussionsHashnode Enterprise
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

How does a Javascript code work?

Atique Akhtar's photo
Atique Akhtar
·Sep 29, 2021·

1 min read

JavaScript is a single-threaded language, meaning only one task can be executed at a time. When the JavaScript interpreter initially executes code, a global execution context is created. A global execution context has two components and it works in two phases.

  1. Creation Phase/Memory Creation Phase.
  2. Code Execution Phase

    Creation Phase/Memory Creation Phase

    In this phase, memories are allocated to all the variables and functions that are used in the code. Initially 'undefined' is assigned to all the variables and in the case of a function, the whole function snippet is assigned to a variable having the same name as the function.

The memory creation phase is the base and the core idea behind the concept of hoisting in Javascript which enables us to access the variables and functions even before declaring them.

Code Execution Phase

This is the phase where the codes are executed and actual values are assigned to the variables replaced with undefined which were assigned in the previous phase of memory creation.

Global execution context is popped out from the call stack, once the execution completes.