Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.