Let with 2 vars i

Tell us what’s happening:

Isn’t “let” supposed to prevent me from creating another variable with the same name and a different value? That’s what I understood from the previous lesson.

I understand the for loop has an individual scope but shouldn’t it inherit the scope of the function it is in?

Your code so far


function checkScope() {
'use strict';
let i = 'function scope';
if (true) {
  let 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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36.

Challenge: Compare Scopes of the var and let Keywords

Link to the challenge:

1 Like

May be {} is another scope

{}s create new scope. Let doesn’t prevent shadowing of variables. (i.e. redefining inside a new scope is ok)

I thought that was part of what it was supposed to do… glad to know I was mistaken.
Thanks!