How JavaScript Works
How JavaScript Works -- Piyu-Jay Tillu

How JavaScript Works

JavaScript is among the most famous languages today. From Web, and Mobile to AI, JavaScript is everywhere. As a developer, you should know the ins and outs of the language. This knowledge will help you understand the language and make your logic more clear. So let's take a look at how JavaScript works.

Everything in JavaScript happens inside an Execution Context also known as Global Context. You can assume this Execution Context as a container inside which whole the JavaScript code is executed. Assume the Execution Context is like a big box and it executes JavaScript code in two phases:

  1. Memory Creation Phase
  2. Code Execution Phase

Memory Creation Phase

In the Memory Creation Phase, all the variables and functions are stored as key-value pairs. The Memory Creation Phase is also known as the Variable Environment.

In the Memory creation phase, the memory will be allocated to all variables and functions inside the Global Execution Context. In this phase, variable declarations are scanned and made undefined.

  • It stores undefined to all variables.
  • For Functions, it stores the entire function code inside memory space.

Code Execution Phase

Code Execution Phase is the phase when code is executed one line at a time. The Code Execution Phase is also known as the Thread of Execution.

JavaScript is a synchronous single-threaded language. JavaScript can only execute one command at a time and in a specific order. It can only go to the next line once the current line has been finished executing.

In the code execution phase, the whole JavaScript program executes line by line. In this phase, all the calculations are done. In this phase, we allocate values to all variables. Previously it had an initial value of undefined.

JavaScript is treated to function as a mini program. So when JavaScript executes a function a new execution context is created. So just like the whole program runs in a Global execution context. To execute a function JavaScript creates a local execution context. Just like the Global Execution context has two phases, this local execution context is also having the same two phases.

After completion of function execution, the result is stored inside the Memory Component. Once the execution is done and the result is stored inside the Memory Component the local execution context is deleted from the code component.

Conclusion

So in this blog, we discuss how JavaScript works by breaking down its execution into two main phases within a Global Context:

  1. Memory Creation Phase (Variable Environment):During this phase, JavaScript allocates memory for variables and functions.Variable declarations are scanned, and they are set to undefined initially.Functions are stored in memory with their entire code.
  2. Code Execution Phase (Thread of Execution):JavaScript operates as a synchronous single-threaded language, meaning it processes one command at a time and in a specific order.This is where the actual code is executed line by line. Calculations are performed, and values are assigned to variables.When a function is called, a new local execution context is created with its own memory and two phases, similar to the global context.After a function finishes executing, its result is stored in memory, and the local execution context is removed.

In essence, JavaScript runs within a Global Context, which acts like a container for code execution, and it follows a two-phase process for managing variables and functions, as well as executing code. Additionally, functions create their own local execution contexts during their execution.

Sounds too complicated? Read the Simplified Versions

Read more about React & JavaScript

Follow me for more such content.


Khalid Farhan

Building HoverConsole.com | Entrepreneur | Visionary

9 个月

Good explanation

回复

要查看或添加评论,请登录

Jay Tillu的更多文章

  • The Parkerian Hexad: A Cybersecurity Framework for the Modern World

    The Parkerian Hexad: A Cybersecurity Framework for the Modern World

    In the ever-evolving landscape of cybersecurity, the traditional CIA triad (Confidentiality, Integrity, Availability)…

    1 条评论
  • TCP/IP Model and Protocol - Simplified

    TCP/IP Model and Protocol - Simplified

    Think of TCP/IP as the language computers use to talk to each other on the internet and other networks. Here's a…

  • How AWS Shield Protects You From DDoS?

    How AWS Shield Protects You From DDoS?

    The threat of cyber attacks is large in today's interconnected digital world, where businesses and individuals rely…

  • Understanding AWS EC2 Pricing Plans

    Understanding AWS EC2 Pricing Plans

    The cloud has revolutionized how businesses approach computing resources. Amazon EC2, a core service of Amazon Web…

  • What is Amazon EC2?

    What is Amazon EC2?

    Amazon EC2 (Elastic Compute Cloud) is a web service provided by Amazon Web Services (AWS) that allows users to rent…

  • What is Cloud Computing?

    What is Cloud Computing?

    Imagine a world where you don't need a bulky desktop PC or a server room full of whirring machines to run your…

  • Call Stack in JavaScript - Simplified

    Call Stack in JavaScript - Simplified

    In JavaScript, the call stack is like a to-do list for functions in your program. It follows the rule of "Last In…

  • Maps in Dart

    Maps in Dart

    Just like a List, a Map is also a type of collection. In Maps data is stored in key: value pairs.

  • What is Dart Mixins?

    What is Dart Mixins?

    In object-oriented programming, mixins have emerged as a powerful tool for enhancing code reusability and organization.…

    1 条评论
  • JavaScript Pass by Value and Pass by Reference - Simplified

    JavaScript Pass by Value and Pass by Reference - Simplified

    Based upon Datatype, JavaScript passes a value of a variable using two ways: Using Pass by Value Using Pass by…

社区洞察

其他会员也浏览了