Lowkey Hugo

Lowkey Hugo

JavaScript Execution Context – How JS Works Behind the Scenes

JavaScript

Have you ever wondered how JavaScript works behind the scenes? It’s actually quite fascinating! And that’s what you’ll learn about here.

JavaScript is a single-threaded interpreted language. Every browser has its own JavaScript engine. Google Chrome has the V8 engine, Mozilla Firefox has SpiderMonkey, and so on. They all are used for the same goal, because the browsers cannot directly understand JavaScript code.

Let’s look at an example so we can learn more:

var n = 5;

function square(n) { var ans = n * n; return ans; }

var square1 = square(n); var square2 = square(8);

console.log(square1) console.log(square2) In the above code,

n is initialized and assigned a value of 5 We defined a function square() that accepts an argument n and returns the square of n. We call the square() function and store the returned value in the square1 variable. We call the square() function and store the returned value in the square2 variable. Finally, it outputs both square1 and square2 Behind the scenes, JavaScript is doing so many things. Let’s understand all of it.