Compare Scopes of the var and let Keywords 2

Tell us what’s happening:
hello i have a little issue of understanding the difference between let and var. So before completing this challenge i quietly didn’t understand that lesson

Your code so far


function checkScope() {
"use strict";
  var i = "function scope";
  if (true) {
    i = "block scope";
    console.log("Block scope i is: ", i);
  }
  console.log("Function scope i is: ", i);
  return i;
}

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/compare-scopes-of-the-var-and-let-keywords

function checkScope() {
"use strict";
  var i = "function scope"; // this is scoped to checkScope
  if (true) {
    i = "block scope"; // right now, this is changing the value of `i` from
                       // line 3 because that's the currently active `i` 
                       // variable _but_ because the `if` has a block scope 
                       // (and that's because of its curly braces `{}`)
                       // you can actually create an entirely new variable 
                       // `i`.  But you can't do so with the keyword `var`.
                       // However, the text of this exercise introduces a 
                       // different keyword to declare a variable, and that 
                       // keyword will let you create a completely new `i` 
                       // in the block scope of this `if`
    console.log("Block scope i is: ", i);
  }
  console.log("Function scope i is: ", i);
  return i;
}

ok i see now thanks metasean

1 Like