5

Just wondering if they are the same thing since some programmers say "scope" while others say "execution context".

1
  • 1
    I use Scope all the time when not trying to be "specification precise". It doesn't encompass all the aspects of an Execution Context, but can be handy to informally discuss variable visibility (lexical scoping). It is also more of a "language neutral" term.
    – user166390
    Sep 21, 2011 at 2:46

2 Answers 2

8

Short answer: an execution context is defined by the spec to consist of 3 components:

  • LexicalEnvironment
  • VariableEnvironment
  • ThisBinding

In general the term "variable scope" is used as a synonym for the LexicalEnvironment component. So, technically speaking, yes, there is a difference. In practice, however, terminology usage varies wildly and does not necessarily line up with the spec.


Longer answer: To the ECMA-262-Spec-cave!

10.3 Execution Contexts

When control is transferred to ECMAScript executable code, control is entering an execution context. Active execution contexts logically form a stack. The top execution context on this logical stack is the running execution context. A new execution context is created whenever control is transferred from the executable code associated with the currently running execution context to executable code that is not associated with that execution context. The newly created execution context is pushed onto the stack and becomes the running execution context.

An execution context contains whatever state is necessary to track the execution progress of its associated code.

...

The LexicalEnvironment and VariableEnvironment components of an execution context are always Lexical Environments. When an execution context is created its LexicalEnvironment and VariableEnvironment components initially have the same value. The value of the VariableEnvironment component never changes while the value of the LexicalEnvironment component may change during execution of code within an execution context.

In most situations only the running execution context (the top of the execution context stack) is directly manipulated by algorithms within this specification. Hence when the terms "LexicalEnvironment", "VariableEnvironment" and "ThisBinding" are used without qualification they are in reference to those components of the running execution context.

...

10.3.1 Identifier Resolution

Identifier resolution is the process of determining the binding of an Identifier using the LexicalEnvironment of the running execution context. During execution of ECMAScript code, the syntactic production PrimaryExpression : Identifier is evaluated using the following algorithm:

  1. Let env be the running execution context‘s LexicalEnvironment.
  2. If the syntactic production that is being evaluated is contained in a strict mode code, then let strict be true, else let strict be false.
  3. Return the result of calling GetIdentifierReference function passing env, Identifier, and strict as arguments.

The result of evaluating an identifier is always a value of type Reference with its referenced name component equal to the Identifier String.

3
  • What's the difference between LexicalEnvironment and VariableEnvironment ? Does the former just mean "scope" ?
    – doubleOrt
    Sep 14, 2017 at 9:26
  • If my former comment's assumption of LexicalEnvironment being correct, then i presume an execution context is anything that 1. has its independent scope 2. has a this binding of its own 3. ??????
    – doubleOrt
    Sep 14, 2017 at 9:30
  • I signed in particularly in order to like your message!
    – Vladimir
    Aug 19, 2021 at 11:18
0

Generally "scope" refers to the interval within which something - e.g., a variable - is in existence or is valid in some way.

"Context" sometimes refers to the set of objects accessible to an object operating with a particular framework as a matter of course - as part of the specification of that framework. That's what Matt provided. But sometimes it means something like "scope". "Within the scope of the current function" means pretty much the same as "within the context of the current function".

You can usually tell which meaning of context is meant from ... context.

I am not a Javascript expert!

1
  • Your post is an example of why technical terms should be used carefully in discussions. The general term "context" could include the general environment, other functions, purpose, etc. whereas the specific term "execution context" is described precisely by ECMA-262. For example, you may see people refer to a function's this keyword as its "context", which is not consistent with either the general or specific meaning—and why I tend to nit-pick on that point. :-)
    – RobG
    Sep 21, 2011 at 5:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.