Review of Week 2 Day 4 Learning Objectives

1. Identify the difference between const, let, and var declarations

2. Explain the difference between const, let, and var declarations

var a = "a";
let b = "b";
const c = "c";

3. Predict the evaluation of code that utilizes function scope, block scope, lexical scope, and scope chaining

Consider this run function, inside which foo and bar have function scope. i and baz are scoped to the block expression.

// function and block scope in this example
function run() {
  var foo = "Foo";
  let bar = "Bar";

  console.log(foo, bar);

  {
    console.log(foo);
    let baz = "Bazz";
    console.log(baz);
  }

  console.log(baz); // ReferenceError
}

run();

Notice that referencing baz from outside it’s block results in JavaScript throwing a ReferenceError.


Consider this run function, inside of which foo has function scope.

function run() {
  console.log(foo); // undefined
  var foo = "Foo";
  console.log(foo); // Foo
}

run();

Consider this func1 function and it’s nested scopes.


// global scope
function func1(arg1) {
  // func1 scope

  return function func2(arg2) {
    // func2 scope

    return function func3(arg3) {
      // func3 scope

      console.log(arg1, arg2, arg3);
    }
  }
}

6. Implement a closure and explain how the closure effects scope

const adder = (arg1) => {
  return (arg2) => {
    return arg1 + arg2;
  }
};

const func2 = adder(2);
const result = func2(2);
console.log(result); // => 4;

```js function