Hoisting & Var Flashcards
(27 cards)
What is var?
Var was the original way of declaring variables in JS
What happens when you use var at the top level of a program?
Usingvarat the top level of a program creates a property on the global object, e.g.,globalin Node orwindowin a browser.
var bar = 42;
console.log(global.bar);
// 42
What happens when you use var inside a function?
When you usevarinside a function, the variable isnotstored as a property of the global object:
function foo() {
var bar = 42;
console.log(global.bar);
}
foo();
// undefined
How is let scoped, how is var scoped? What is the difference?
letisblock-scoped, whilevarisfunction-scoped.
A block-scoped variable is only visible within the block where it is declared; A function-scoped variable is visible within the function where it is declared.
function foo() {
if (true) {
var a = 1;
let b = 2;
}
//what do these log?
console.log(a);
console.log(b);
}
foo();
// 1
// ReferenceError: b is not defined
What are the types of scope?
Visibility Scope
Declared Scope
Lexical Scope
What is visibility scope?
Visibility scope refers to where a particular identifier – a variable, function, or class name – is available for use by your code. If a variable is available throughout your code, then it has global scope. Otherwise, it has local scope.
What are the two ways a variable can be scoped when talking about visibility scope?
If a variable is available throughout your code, then it has global scope. Otherwise, it has local scope.
What is the visibility scope for the following variables?
let foo1 = 1;
var bar1 = 2;
if (true) {
let foo2 = 3;
var bar2 = 4;
}
let foo1 = 1; // visibility scope is global
var bar1 = 2; // visibility scope is global
if (true) {
let foo2 = 3; // visibility scope is local (local block)
var bar2 = 4; // visibility scope is global
}
What is the visibility scope for the following variables?
function xyzzy() {
let foo3 = 5;
var bar3 = 6;
if (true) {
let foo4 = 7;
var bar4 = 8;
}
}
function xyzzy() { // visibility scope is global
let foo3 = 5; // visibility scope is local (local function)
var bar3 = 6; // visibility scope is local (local function)
if (true) {
let foo4 = 7; // visibility scope is local (local block)
var bar4 = 8; // visibility scope is local (local function)
}
}
What is declared scope?
Declared scope refers to how a particular identifier is declared. For instance, we use theletkeyword to declare variables with block scope, and usevarto declare variables with function scope. Knowing the declared scope lets us determine where a variable is available.
What are the two scope types when talking about declared scope?
we use theletkeyword to declare variables with block scope, and usevarto declare variables with function scope.
ID the declared scopes
let foo1 = 1;
var bar1 = 2;
if (true) {
let foo2 = 3;
var bar2 = 4;
}
let foo1 = 1; // declared scope is block scope
var bar1 = 2; // declared scope is function scope
if (true) {
let foo2 = 3; // declared scope is block scope
var bar2 = 4; // declared scope is function scope
}
ID the declared scopes
function xyzzy() {
let foo3 = 5;
var bar3 = 6;
if (true) {
let foo4 = 7;
var bar4 = 8;
}
}
function xyzzy() { // declared scope is function scope
let foo3 = 5; // declared scope is block scope
var bar3 = 6; // declared scope is function scope
if (true) {
let foo4 = 7; // declared scope is block scope
var bar4 = 8; // declared scope is function scope
}
}
What is lexical scope?
Lexical scope can refers to the structure of your code. The lexical scope distinguishes between variables that are declared inside a function or block (inner scope) and the variables that are declared outside of that function or block (outer scope). Lexical scope is especially important with closure
ID the lexical scope
let foo1 = 1;
if (true) {
let foo2 = 3;
}
function xyzzy() {
let foo3 = 5;
if (true) {
let foo4 = 7;
}
}
let foo1 = 1; // outer scope of xyzzy, outer scope of if block on line 3
if (true) {
let foo2 = 3; // inner scope of if block on line 3
}
function xyzzy() {
let foo3 = 5; // inner scope of xyzzy, outer scope of if block on line 10
if (true) {
let foo4 = 7; // inner scope of if block on line 10
}
}
ID the lexical scopes
var bar1 = 1;
if (true) {
var bar2 = 3;
}
function xyzzy() {
var bar3 = 5;
if (true) {
var bar4 = 7;
}
}
var bar1 = 1; // outer scope of xyzzy, outer scope of if block on line 3
if (true) {
var bar2 = 3; // outer scope of xyzzy, outer scope of if block on line 3
}
function xyzzy() {
var bar3 = 5; // inner scope of xyzzy, outer scope of if block on line 10
if (true) {
var bar4 = 7; // inner scope of xyzzy, outer scope of if block on line 10
}
}
What are the two main phases of JS engine operation? How does this relate to hoisting?
JavaScript engines operate in two main phases: acreation phaseand anexecution phase. The execution phase occurs when the program runs code line-by-line. before the execution phase begins, the creation phase does some preliminary work. One of those work items is to find all of the variable, function, and classdeclarations. When it encounters each of these identifiers, it records the name and designates its scope. When the execution phase begins, JavaScript knows what variables exist and where they are in 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 processhoisting.
Are variables hoisted?
Variables declared with thelet,const, andvarstatements are also hoisted
What happens when a var is hoisted?
when a varvariable is hoisted, JavaScript gives it an initial value ofundefined. Thus, you can access it and it will give you undefined.
What happens when let/const variables are hoisted?
Whenlet
andconst
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.” Such unset variables are said to be in theTemporal Dead Zone, or theTDZ. Such variables remain in the TDZ until the initialization code runs during the execution phases.
If you try to access alet
orconst
variable that is still in the TDZ, you’ll get an error:
Are function declarations hoisted?
JavaScript also hoists function declarations to the top of the scope. In fact, it hoists the entire function declaration, including the function body