6. Block Scope III - A question


#1
const starCount = () => {
  let i = 5;
  console.log(i);
  
  for (let i = 0; i < 12; i++) {
    console.log(i);
  }
};

starCount();
console.log(i);

Question: Is the variable “i” in the “for” loop completely different from the one at the top of the function, or are they the same but the “for” one is overriding the first one?

i’m asking this, because if I remember correctly, in other languages(I think C#) you can have different variables with the same name as long as they’re not sharing the same scope.

(also, an aside: This exercise asks for the student to create a “For loop” even though it hasn’t been introduced yet. Following the Syllabus, “Loops” come after “Scope”.)


#2

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.