Section 2: Execution Context and Lexical Environments Flashcards
What is a Syntax Parser
- A program - Read the code - Determines what it does Check if grammar is valid
What is a Lexical Environment
Where something sits physically in the code Were code is written is important
In this code, where is sitting “var a” lexically
function hello() {
var a = “hello world”;
}
inside the function
What is an Execution Context
A wrapper to manage the code that is running. The section of code that is running. There are lots of lexical environments. Which one is currently running is managed via execution contexts. It can contain things beyond what you’ve written in your code
What is a Name/Value Pair?
Is a name which maps to a unique value.
In a name/value pair, can we define more than one name/value pair?
Yes
In a name/value pair, a name, can hold more than one value in any given context?
No, just one value
in any particular execution context, a name can only exist and be defined with one value?
Yes
in any particular execution context, a name/value pair, can hold more name/value pairs?
Yes
what is an An Object in JS?
Is a collection of name/value pairs(and that value can be a collection of more name/value pairs).
Whenever code is run in JS, where do we say it´s running?
Inside an execution context
What is the base execution context?
the global execution context It´s accesible everywhere to everything in the code
A global execution context creates what things?
A global object(window) A special variable called this
at the global level, what object and keyword are equal?
the window object, and “this”
When we mean Global in JS we mean, what?
Not inside a function
The execution context is created in how many phases?
2 phases
Name the two phases of an execution context?
1st: Creation phase 2nd: Execution phase
What happens in the Creation phase(execution context) in the global environment
- We set up the global object within memory
- creation of “this” keyword
- An outer environment set to null
- variables are set, with a value of undefined
- functions definitions are set with their entire code
In the creation phase(execution context) the parser recognize what? and sets up what?
were you´ve created variables and functions and sets up memory space for variables and functions
In the creationg phase(execution context) a function how is stored in memory?
its entirety placed into memory, its name and code
What is undefined in JS?
It’s actually a special value that JS has within it internally that means that the variable hasn’t been set
What’s happening here: console.log(a) // it’s undefined var a = 5; console.log(a) // 5
When we declare “a”, it’s placed into memory during the creation phase. So the execution context saw “var a” and set up “a” in memory, and so even though i haven’t set it to a value, the JS engine, already set it to this special value called undefined.
What is happening here:
console.log(a) // a is not declared -> “Uncaught ReferenceError: a is not defined”
That’s because that initial execution context creation, that creation phase, when it went through it didn’t find a “var a” so it never set up the memory space. So when it went to execute this code it said “hey, I don’t have a in memory at all, so it gave you an Uncaught Reference, a is not defined. I don’t have it in memory”
What is the name of the second phase in the execution context?
Execution phase
what happens in the execution phase?
In the execution phase we already have all those things set up that we had before, and now it runs your code, the code you’ve written, line by line, interpreting it, converting it, compiling it, executing it on the computer into something the computer can understand.
What means single threaded
One command is executed at a time JS isn’t the only thing happening in the browser, so when we say JS is single threaded, it may not be under the hood of the browser, but from our perspective as programmers, JS behaves in a single threaded manner.
What it means Synchronous execution
In the purposes of programming, one at a time. One line of code being executed for synchronous execution at a time and, in the order that it appears.
what it means single threaded synchronous execution
In JS, only one thing is happening at a time.
What is invocation in JS
Running or calling a function, in JS by using the parenthesis()
what is the execution stack?
one execution context on top of the other on top of the other, And whichever one is on top is the one that’s currently running. So any time you execute or invoke a function in JS, a new execution context is created and put on the execution stack

How function invocation happens in JS
Every function creates a new execution context which runs through the create phase, then the execution phase were it executes the code line by line within the function
What are Variable Environments?
Were the variables lives, and how they relate to each other in memory.
What happens with a variable that it´s created inside a function?
Every variable created and settled inside a function, it will live inside that execution context(the function execution context) and it will be different from others functions and the global execution context.
Every variable is attached to the variable environment of their own execution context. It’s the same to say that every execution context has their own variable environment.
Every execution context has a reference to its outer environment?
Yes
What is the outer environment?
And explain what happens when a variables is called in a function, and how this relate to the outer environment
The outer environment it’s related to the Lexical environment, it’s, where the function sits lexically (phisically in the code)
When a function searches for a variable, it will look first for the variable in their own variable environment on their own execution context, if it’s not found, it will take a look at the reference to the outer environment , and look at the variable at that point.
When the outer reference is created?
When a function invocation takes place.
How it’s call to the act of searching for a variable in the chain of references to outer environments
The scope chain
In terms of Scope Chain
What scope means
and
What Chain means
Scope means “where a variable is available in your code, and if’s truly the same variable, or a new copy”
Chapin means “those links of outer environments references”
Name anoteher way of thinking about “the reference to the outer environment”
Who created me?
the outer reference is to the execution context in which the function was created
“la referencia externa es hacia el contexto de ejecución en donde la función fue creada”
In ES6 exist new way of declaring a variable, what is it? and what’s the purpose, and when can we access that variable?
its “let”
Purpose: allows the JS engine to use what’s called block scoping.
We can access a variable declared with “let” only after we declared the variable,if we called before, we get an error
A variable declared with “let”, if we called before its declared, we can´t access it, but it’s already in memory?
Yes, its in memory, but the engine just won´t allow to access it.
where lives a “let” variable, and what happens if we are using it inside a loop?
it lives inside a block, a block is defined by curly braces { }
If we use it inside a loop, we are going to get a different variable in memory each time the loop is running