6

Does JavaScript create a new execution context when executing a block to associate the its lexical environment with it.

2
  • Check section "13.2.14Runtime Semantics: BlockDeclarationInstantiation" in ecma-international.org/ecma-262/8.0/… Commented Oct 1, 2017 at 18:12
  • Are you referencing a block outside of a function call or a block within a function call? Commented Oct 1, 2017 at 18:45

1 Answer 1

10

No. An execution context is essentially a call stack frame, while the lexical environment is the current scope. No function gets called when a block is evaluated.

You can read in the specification for the evaluation semantics of blocks that it creates a new lexical environment (initialised with the variables in the block scope) that has the old environment as its parent, and "Set[s] the running execution context’s LexicalEnvironment to [that value]". After executing the statements in the block, the child environment is popped off again, but the running execution context did stay the same all the time.

Sign up to request clarification or add additional context in comments.

7 Comments

After the execution of the block is finished, LexicalEnvironment will be set again to the lexical environment of the function block right ?
It will be set back to the lexical environment that was the execution context's lexical environment before entering the block statement. That's not necessarily the one of the function, it's whatever scope the block is found in.
even in the dev tools, you can see with block scope there is NO new callstack that gets created
@Daniel Yes, the braces around function bodies don't form a block. The function body still gets its own scope though.
@Daniel Exactly
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.