A new execution context is created for each function in JavaScript.
How many execution contexts are present in memory when the following code is run? Note that function Bar
is not invoked.
function Foo () {
function Bar() {}
}
Foo();
Also, when are the execution contexts created? At evaluation time or runtime?
The runtime invocation of a function is what causes an execution context to be created. In your example, therefore, there's only one function call, so only one execution context is involved.
The static (compile-time) arrangement of functions is important, because that determines scope and the eventual contents of execution contexts. It's the actual call to a function that matters, however, for the creation of a context. (Some older languages used the term "activation record", though that may have been more intended for stack-based allocations.)
You can read details in the sometimes turgid language of the spec, though it can be hard to make out the forest for the trees. The spec is written in terms of control being transferred. A function call is a very common way that that happens, but so is the invocation of an event handler, or the invocation of a complete
<script>
block when it is initially loaded by a browser.