Adv Conc- var, scope, hoist, strict Flashcards
what does var do in the global context?
puts a property on the global object
what does var do in a block?
puts a property on the global object
what does var do in a function body?
create a local variable. var has function scope
What does this do:
function foo() {
if (true) {
var a = 1;
let b = 2;
}
console.log(a);
console.log(b);
}
foo();
console.log(a); // 1
console.log(b); // ReferenceError: b is not defined
var creates a variable with function scope, while let creates a variable with block scope. Thus, a is available everywhere in the function, but b is only available in the block.
function foo() {
if (false) {
var a = 1;
}
console.log(a);
}
foo();
console.log(a); // undefined
Even though the code on line 3 never runs, we still create a variable named a with function scope. Furthermore, since we’re not initializing a, it receives a default value of undefined instead of 1. Thus, line 6 displays undefined.
What happens in the execution phase?
How does it treat function declarations?
The program runs code line-by-line.
Function declarations are sort of “skipped” or rather they’ve been moved to the creation phase
What happens during the creation phase? (2 things)
What does it feel like to the developer?
Find all the variable, function, and class DECLARATIONS, function declarations bodies are found as well
It determines their scope
From the developer’s perspective, the code acts like the declarations were moved to the top of their respective scope. In particular, function-scoped declarations are moved to the function’s beginning, and block-scoped declarations are moved to the block’s start. We call this process hoisting.
What’s different about var vs let/const in terms of hoisting?
JS assigns var variables an initial value of undefined
If you try to access the value assigned to a var variable before the original statement with the var declaration gets executed, JavaScript returns undefined.
console.log(bar); // undefined
var bar = 3;
console.log(bar); // 3
When let and const variables are hoisted, they are not given an initial value at all. Instead, they are left in an “unset” state; that is, they are “not defined.” (Don’t say “undefined,” though - that’s confusing since undefined is also a value.) Such unset variables are said to be in the Temporal Dead Zone, or the TDZ. Such variables remain in the TDZ until the initialization code runs during the execution phases.
What’s the TDZ?
Temporal Dead When let and const variables are hoisted, they are not given an initial value at all. Instead, they are left in an “unset” state; that is, they are “not defined.” (Don’t say “undefined,” though - that’s confusing since undefined is also a value.) Such unset variables are said to be in the Temporal Dead Zone, or the TDZ. Such variables remain in the TDZ until the initialization code runs during the execution phases.
let, const, and class will put variables into TDZ
(var doesn’t seem to enter the tdz)
How will these error messages differ
console.log(baz);
console.log(qux);
const qux = 42;
// ReferenceError: baz is not defined
// ReferenceError: Cannot access ‘qux’ before initialization
What happens with this:
function foo() {
return bar();
function bar() {
return 42;
}
}
42
What is this code equivalent to?
var bar = ‘hello’;
bar();
function bar() {
console.log(‘world’);
}
// raises “TypeError: bar is not a function”
function bar() {
console.log(‘world’);
}
bar = ‘hello’;
bar();
What is this code equivalent to?
bar();
var bar = ‘hello’;
function bar() {
console.log(‘world’);
}
// logs “world”
function bar() {
console.log(‘world’);
}
bar();
bar = ‘hello’;
What happens when a var variable and a function declaration have the same name? like:
var bar = 1;
bar();
function bar() {
console.log(2);
}
In that case, the function declaration gets hoisted to the top of the program and the variable declaration gets discarded. (Some people say that the function declaration gets hoisted above the variable declaration, but it’s more correct to say that the variable declaration gets discarded.)
How to avoid the hoisting subtleties of var/let/const/functions/etc (4 things)
use let and const
put vars at the top
use let and const as close to first usage as possible
declare functions BEFORE calling them even though they’re hoisted
What happens with this:
let foo = “hello”;
function foo() {
console.log(“hello”);
}
what line throws the error
The error occurs on line 3, not line 1.
What happens with this:
what line throws he error
function foo() {
console.log(“hello”);
}
let foo = “hello”;
This time, the foo function is seen first during the creation phase, so the error doesn’t occur until JavaScript reaches line 5.
Is hoisting an actual thing?
no but:
In the remainder of the curriculum, we will talk about hoisting as an actual process. You should consider that as a given in future assignments, quizzes, and assessments. If we ask you about hoisting, don’t try to argue that there is no such thing.
Keep in mind that we may ask you to explain how hoisting really works, so don’t neglect this section.