Mathew
Mathew

Reputation: 129

Is the concept of the "Execution context and the stack" is just for the browsers?

Is the concept of the "Execution context and the stack" is just for the browsers? or it's also implemented in other places such as NodeJS? I've written 2 sentences and I'm not sure if they're correct:

1- "it’s not the “browser “who’s the environment for JavaScript! in other words: it’s not the entire browser! it’s just a part from the browser called The execution contexts."

2- All JavaScript code needs to run inside an environment and in the case of the browser this environment is called "execution contexts"

Upvotes: 0

Views: 103

Answers (2)

Eriks Klotins
Eriks Klotins

Reputation: 4180

In short no, and you are mixing up different things.

In simplified terms, the execution context is a memory area accessible to the program. A program may consist of subprograms (class methods/functions/procedures) each of these has its own execution context to keep local variables local.

A stack in memory management is a mechanism used to keep track of different execution contexts. When you call a function, the current execution context gets pushed to a stack to offer a clean slate for the function to run. After the function is completed, the stack pops and returns the previous context.

These principles of memory management are true for computers, browsers, toasters, iPhones, and programs written in assembly language, and programs in Scratch.

Upvotes: 1

Elliot Nelson
Elliot Nelson

Reputation: 11557

JavaScript typically runs inside a JavaScript engine.

As an example, Google's "V8 JavaScript Engine" powers javascript running in the Chrome browser, and in NodeJS.

Another example would be JavaScriptCore, Apple's engine, which runs javascript in the Safari browser and in various other contexts in MacOS / iOS.

Another example is Chakra, Microsoft's engine, which runs javascript in IE and Edge, or the javascript you would write in an "HTML application" published on XB1.

Even though JavaScript in Chrome and NodeJS run in the same engine, they have different additional modules and capabilities built around them -- things like the fs module in NodeJS, or access to the running DOM in the browser -- that are accessible from your code if the javascript agent provides it.

The "execution context and the stack", which describes the way JavaScript handles function calls, should be similar in every JavaScript engine because it is described by the ECMAScript spec. But if you get down into the nitty gritty, there can definitely be subtle differences in the way the stack is generated or formatted between V8 and JavaScriptCore, for example.

Upvotes: 1

Related Questions