Understanding the Working of the JavaScript

Understanding the Working of the JavaScript

Ever wondered how JavaScript code actually runs behind the scenes? Understanding the JavaScript execution process is crucial for writing optimized and efficient code. In this article, we’ll break down how the JavaScript engine, particularly Google’s V8 engine, processes and executes JavaScript code.


How JavaScript Works?

  1. JS Engine:

  • It interpret and execute the JavaScript Code.
  • Every Browser has its own JS Engines. Some of them are :
  • V8(Chrome,Node.js)
  • Spider Monkey(Firefox)
  • JS Core(Safari)
  • Chakra(old version of Edge)

2.Execution Process:

In this the execution of the code happened

Step1:Parsing and Compilation (Just-In-Time Compilation):

  • JS Engine parses the code into an Abstract Syntax Tree(AST).
  • After that AST Compiles into bytecode using JIT Compilation for execution .

Step2:Execution Context Creation:

1. Global Execution: It creates when the Scripts start running.

2.Function Execution Context: It creates at the time of Function Calling.

3.Memory Heap: It Stores Variables and function Objects.

4.Call Stack: It keeps the track of function execution order.

Step3:Event Loop and concurrency model:

As we all know JS is a single threaded language means it runs one task at a time . However it can handle Asynchronous(like Set Timeout , Fetch, Event listener and many more) operations using

  • Call Stack: It Execute the synchronous code.
  • Web APIs: It Handles Asynchronous Operations .
  • Callback Queue: It Stores Call back Functions for later execution .
  • Event Loop: It moves the task from the Callback queue to call stack when the call stack is empty.

Conclusion:

JavaScript’s execution model, especially with the V8 engine, is designed for speed and efficiency. From parsing and JIT compilation to event loops and concurrency models, understanding these concepts helps developers write better, optimized code.

What are your thoughts on JavaScript’s execution process? Let’s discuss in the comments!


Mridul Goswami

Student at SUNDERDEEP ENGINEERING COLLEGE, DASNA, GHAZIABAD.

3 周

good explanation ??

回复

Informative ????

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

Ansh Shukla的更多文章

社区洞察

其他会员也浏览了