DEV Community

Cover image for JavaScript Under the Hood: Understanding the Core Mechanics ✨
Alaa Samy
Alaa Samy

Posted on

JavaScript Under the Hood: Understanding the Core Mechanics ✨

JavaScript is an incredible language that powers the web, yet many of us interact with it daily without understanding what’s happening behind the scenes. Let’s dive into the mechanics of how JavaScript works under the hood and uncover the magic that makes it so dynamic.

The Foundation: Single-Threaded Execution

At its core, JavaScript is a single-threaded, synchronous language, meaning it processes one task at a time, line by line, in a single thread.

Single-Threaded Execution

Despite being single-threaded, JavaScript has asynchronous capabilities that make it appear multi-threaded.
This is possible through Web APIs provided by browsers (or Node.js in server environments), which allow operations to happen outside the main thread.

Multi-Threaded Execution


JavaScript's Runtime Anatomy:

Every JavaScript runtime consists of three key components that work together:

1- Call Stack:

  • A data structure that tracks the functions being executed. It follows the LIFO (Last In, First Out) principle—functions added last are removed first.
  • At the bottom of the stack is the Global Execution Context, the environment for global code, and as the functions are invoked, their execution contexts are stacked on top.

2- Memory Heap:

  • Stores objects, arrays, and other complex data structures in memory during program execution.
  • JavaScript uses automatic garbage collection to free memory when objects are no longer referenced.

3- Execution Context:

  • An execution context is a special environment created to handle the transformation and execution of JavaScript code. It contains the currently running code and everything that aids in its execution.
  • JavaScript has a Global Execution Context for the main script and Function Execution Contexts for each invoked function.

- Phases of Execution Context:

1- Memory Creation Phase:

  • Create the global object (window in browsers, global in Node.js).
  • Bind the this keyword to the global object.
  • Create the variable environment and process variable declarations:
    • For var declarations: allocate memory and initialize with undefined (hoisting)
    • For let and const declarations: allocate memory but leave in temporal dead zone (uninitialized)

2- Execution Phase:

  • Execute the code line by line.
  • Assign actual values to variables.
  • Create new execution contexts for function calls.

Asynchronous JavaScript: Event Loop & Queues

JavaScript's asynchronicity is managed through:

Task Queue (Macrotask Queue)

  • A FIFO (First In, First Out) queue that holds callbacks from asynchronous operations ready to be executed.

Microtask Queue

  • A higher-priority queue for promises and other microtasks that need to be processed before the next task.

Event Loop

  • The event loop is what makes asynchronous programming possible in JavaScript:
  • It continuously checks if the call stack is empty.
  • If the stack is empty, it looks at the microtask queue first.
  • After microtasks are processed, it checks the task queue.
  • It takes the first callback from the queue and pushes it onto the call stack.

Event Loop & Queues


Conclusion

Understanding JavaScript's inner workings helps you write more efficient code and debug complex issues. From its single-threaded nature to the event loop, and execution contexts, these fundamental concepts form the backbone of JavaScript's behavior.

By mastering these concepts, you'll gain deeper insights into how your JavaScript code is actually executed and be better equipped to write optimal, high-performance applications.

Happy Coding 😊

Top comments (2)

Collapse
 
anwaar profile image
Anwar • Edited

Thanks for the write-up , animation and effort.
What tools do you use for animation ?

Collapse
 
alaa-samy profile image
Alaa Samy

I am glad that you liked the content,
Actually, I got this image from Google ☺️