Compare Scopes of the var and let Keywords. Confusing

Tell us what’s happening:

I don’t understand the method of declaring a function inside a for loop like this. So far FCC has only taught you one method of declaring a fucntion is:

function abcxyz() {

Now suddenly in this example they declare a function like this:

abc = function() {
… code …

And at the end they call the function: abc();

I just don’t get it.

Your code so far

var printNumTwo;
for (var i = 0; i < 3; i++) {
  if(i === 2){
    printNumTwo = function() {
      return i;
// returns 3

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36 OPR/62.0.3331.116.

Link to the challenge:

there are a few differences between the two, but at this level you can consider these the same:

let myFunc = function() {};
function myFunc() {};

if you are interested in an in depth explanation on the differences you can look at this article:

1 Like