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 Using let with for Loops

I didnt get any part of this tutorial. I am completely lost. Can anyone plz break down the code..!

I didnt get any part of this tutorial. I am completely lost. Can anyone plz break down the code..! Specially where the instructor tells the difference between 'var' and 'let'. How does the 'i' remain equal to 10 by using var.?

4 Answers

Samuel Ferree
Samuel Ferree
31,722 Points

The difference between 'var' and 'let' is what's known as block level scoping, in laymans terms, variables defined with 'let' only exist in between the pair of curly braces that starts right before the variable is defined. See below

var x = 2; //This variable exists from here on out
{
  var y = 3; //This variable also exists for the rest of the execution
  let z = 4; //This variable only exists inside the outer curly braces
  {
    //all variables exists here, even z, since this scope is 'inside' the parent scope
  }
}
// x and y can be used here, 
// but we are no longer in the scope in which z was declared with 'let'

Hope this helps.

Akash Sharma
seal-mask
.a{fill-rule:evenodd;}techdegree
Akash Sharma
Full Stack JavaScript Techdegree Student 14,147 Points
for(var i = 0; i < buttons.length; i++) {
          const button = buttons[i];
          button.addEventListener("click", function() {
              alert("Button " + i + " Pressed");
          });
      }

Remember that i++ in the for loop will executed right before another loop iterates, but since 10 < 10 is false loop will terminate and now every button has an event listener that we programmed for an alert. The problem is that since var i is global scope and now any other variable that used it will have i = 10.

Prabal Dutta
Prabal Dutta
304 Points

How is buttons.length = 10?

Bader Alsabah
Bader Alsabah
4,738 Points

On the first loop cycle, = i=0, and then the 2 lines of code are run, on : alert ("button " + i + " Pressed"); /// if this is executed on first iteration of the loop, i should be 0 and not 10. The for loop is curly braced to include both lines of code - so as per my understanding they will execute each time the loop runs, and in each execution the value of i will be less than 10.

What am I missing?

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Hi Sal,

Although it is marked as "Beginner", this is a Workshop and not a course, so it assumes that you have at least a working knowledge of the programming language in which the feature of the workshop is based in.

Looking at your question, I'm assuming that you are new to coding and to JavaScript. The in-depth answer to your question does go a bit beyond the scope of a Community Forum post....
Instead, I think you will find the Digital Literacy Track very interesting and informative. This track is specifically designed to give you an introductory working knowledge of computers and coding, which will be greatly beneficial as you progress with your coding journey.

Give it a look and Keep Coding! :) :dizzy:

Thankyou Jason,

I do have a working knowledge of the programming language. I have been learning from other sites and online tutorials but I just heard about treehouse and signed in to its free trial. So i came across this tutorial and did not get anything the instructor is saying.

Regards.

Steve Gallant
Steve Gallant
14,943 Points

I have to agree with Sal that this video fell short of explaining the "why" very well.

Jacques Butcher
PLUS
Jacques Butcher
Courses Plus Student 4,102 Points

I also don’t get why var would be different than let in this case.