Let and Var


#1

https://www.codecademy.com/courses/introduction-to-javascript/lessons/variables/exercises/string-interpolation?action=resume_content_item
Difference between let and var??


#2

let has block scope:

if ( true ){
   let blockScope = true;
   var functionScope = true;
}
console.log(functionScope);

blockScope only exist within the if clause


#3
function foo () {
    var foo = 42;
    return [foo/6, foo/7];
}
for (let i = 0; i < foo().length; i++) {
    console.log(foo()[i]);
}
// 7
// 6

The above is oddly enough a demonstration of total garbage collection. The variable in the function is collected, and the iterator variable, i, is collected as a result of block scope. We use the function for length and data poll but neither leave a memory trace.


#4

There is such a thing as an inline anonymous block which is perfectly valid but only useful for pasting a collection of code into the command line to execute like a function.

{
  function foo () {
      var foo = 42;
      return [foo/6, foo/7];
  }
  for (let i = 0; i < foo().length; i++) {
      console.log(foo()[i]);
  }
}

The instant I past that in and Enter it the response appears. The earlier mentioned had to be pasted in two separate operations. The anonymous block solves that.