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

Use of <= after renaming the variable "counter" to "i" @2:55

I'm confused if there was a reason why Dave changed "counter < 10" to "i <= 10" at 2:55 in the video. He presents this as a simple renaming of the variable "counter" to the new variable name "i", and talks about how it is common to use single-letter variables when working with loops. Simple enough, just substitute "i" in place of "counter", however, he uses a different comparison operator. In the initial example it is <, but in the example with a single letter variable, this changes to <=. Is there any reason for this change? It seems like it would change the way the code functions, as this would include 10, rather than excluding 10, so it is no longer a straight forward example of shortening the name of a variable.

1 Answer

It was just an example, but there was no reason to change < to <=. You are correct, <= changes the way code works. You can test it in the console.

for (var counter = 1; counter < 10; counter +=1) {
  console.log(counter);
}
console.log("Separator");

for (var i = 1; i <= 10; i++) {
  console.log(i);
}

First time I got numbers from 1 to 9(including 9 I mean). Second time, when using <= I got numbers from 1 to 10(including 10).

Thanks Tom! Just wanted to make sure that I wasn't missing something.