Hoisting & Var Flashcards
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
}