Variable scope, closure Flashcards
{
let message = “Hello”; // only visible in this block
alert(message); // returns
}
hello
{
let message = “Hello”; // only visible in this block
}
alert(message); // returns?
Error: message is not defined
{
let message = “Hello”;
alert(message);
}
{
let message = “Goodbye”;
alert(message);
}
both return?
Hello
Goodbye
let message = “Hello”;
alert(message);
let message = “Goodbye”;
alert(message);
// Error: variable already declared
if (true) {
let phrase = “Hello!”;
alert(phrase); // return?
}
Hello
if (true) {
let phrase = “Hello!”;
}
alert(phrase); //
Error, no such variable!
The Lexical Environment object consists of two parts:
Environment Record – an object that stores all local variables as its properties (and some other information like the value of this).
A reference to the outer lexical environment, the one associated with the outer code.