How JavaScript Code is Executed ??

How JavaScript Code is Executed ??

How JavaScript Code is Executed ??

JavaScript is a synchronous, single-threaded language, meaning it processes one task at a time in a specific order.

Code Execution Steps:

1?? Creation of Execution Context:

  • When the code starts running, the Global Execution Context (GEC) is created.
  • This context has two phases: (a) Memory Allocation Phase:Variables (var n, square2, square4) are allocated memory and assigned the value undefined.Functions (square) are stored in memory as-is. (b) Code Execution Phase:The code runs line by line.Variables get their actual values, and when functions are invoked, a Function Execution Context (FEC) is created.

2?? Function Execution Context:

  • Whenever a function is called, a new Execution Context is created for that function.
  • This has its own memory and code execution phases.
  • When a return statement is reached, the function’s result is passed back, and the FEC is deleted after the function finishes.

3?? Call Stack:

  • JavaScript uses the Call Stack to manage execution.
  • The Global Execution Context is added at the bottom of the stack.
  • When a function is called, its context is added to the stack. Once the function is done executing, its context is removed.
  • When all tasks are completed, the Global Execution Context is removed, and the program finishes.

  1. Memory Allocation Phase:
  2. Code Execution Phase:
  3. Call Stack:

Key Takeaway:

JavaScript efficiently manages execution using the Execution Context and Call Stack, ensuring smooth and predictable code flow! ???

#JavaScriptExecution #CallStackExplained #FrontendDevelopment #WebDevelopment #CodingLife #JavaScriptJourney #TechExplained #CodeTips #ProgrammersLife #MERNStack #NextJsDev #Javascript #Remix js

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

Sonu Kumar的更多文章

社区洞察

其他会员也浏览了