Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Getting Started With ES2015 Defining Variables With let and const Using let with for Loops

Aakash Srivastav
seal-mask
.a{fill-rule:evenodd;}techdegree
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 Points

Using let with for loop

I have a doubt here with const .
If the value of button is changing after every iteration i,e button 0 , button 1 , then why he used const to declare button ?

3 Answers

Steven Parker
Steven Parker
229,785 Points

In this video, "button" is assigned inside a loop. Before the loop repeats, "button" is disposed (no longer exists).

On the next iteration, a new "button" is created. The old one is not being re-assigned.

Aakash Srivastav
seal-mask
.a{fill-rule:evenodd;}techdegree
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 Points

In the previous case , the variable status was a part of login_info object.
But here it's a seperate variable.
And if it's the only property of the button that is changing , then how will it be reassigned so that we will get a Uncaugh type error .
Please help me with example.
I am sorry . I am still confused.

Steven Parker
Steven Parker
229,785 Points

Each iteration has a different context. So this:

for (var i=0; i < 3; i++) {
  const tester = i;
  console.log(tester);
}

Is essentially the same thing as this:

{
  const tester = 0;
  console.log(tester);
}
{
  const tester = 1;
  console.log(tester);
}
{
  const tester = 2;
  console.log(tester);
}

In both cases, the constant(s) exist only between the braces.

Aakash Srivastav
seal-mask
.a{fill-rule:evenodd;}techdegree
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 Points

Oh . Thanks
I just got confused with the word "const" , I forgot that the value gets stored in stack . And it also vanished into next repitition.
Thanks again .