Özgün Bal
Software Developer, Javascript Enthusiast
Scope & Closure
rules for variable look-up
Failed variable look-up => ReferenceError
function haveScope (x) {
console.log(x)
}{
// belongs to upper/global scope
var a = 'variable'
}
for (var i = 0; i < 5; i++) {
console.log(i)
} {
console.log(a); // ReferenceError
console.log(b); // ReferenceError
let a = 5;
const b = {prop: 'value'};
}// 1 and 2 have same effect
/* 1 */
var a;
console.log(a);
a= 2;
/* 2 */
console.log(b);
var b = 3;
function addNumber(num) {
return function(anotherNum) {
return num + anotherNum;
}
}
var addTwo = addNumber(2);
addTwo(3) // remembers num as 2 and returns 3By Özgün Bal