💡 How Does JavaScript Work Behind the Scenes?

💡 How Does JavaScript Work Behind the Scenes?

Many developers use JavaScript every day — but have you ever wondered what really happens behind the scenes when you write something like console.log("Hello")?

Let me explain it simply 👇

🧠 Execution Context

JavaScript runs code inside something called an Execution Context. It's the environment where the code is executed. Every time you run your program or call a function, JavaScript creates a new context.

There are three main types:

  • 🌍 Global Execution Context: Created when your script starts.
  • 🔁 Function Execution Context: Created each time a function is called.
  • ⚠️ Eval Execution Context: Rarely used, comes from eval().

Each context contains:

  • Variables and functions in memory
  • A reference to outer scope (Lexical Environment)
  • The value of this

🧱 Stack-Based Execution

JavaScript uses a Call Stack. When a function is called, its context is pushed to the stack. When it finishes, it is removed. Simple!

Example:

function greet() {
  console.log("Hi!");
}
greet();        

  • ➡️ Global context is created
  • ➡️ greet() context is pushed
  • ➡️ console.log() runs
  • ➡️ Contexts are removed in reverse order

🔍 So What Happens When You Run console.log()?

  • console is an object provided by the browser or Node.js.
  • log is a function written in native code (not JavaScript!).
  • JavaScript passes your message to this function, and it’s printed in the console.

Understanding Execution Context and the Call Stack is the first step to mastering JavaScript internals. 💪

Do you want me to share more about how V8 works, or how async code (Event Loop) runs behind the scenes? Let me know! 🧵👇

#JavaScript #WebDevelopment #Programming #Frontend #TechExplained

To view or add a comment, sign in

Others also viewed

Explore topics