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 trialCristina Davis
10,860 PointsWhy is this an Infinite Loop? Doesn't the counter++ adds?
let counter = 0;
while ( counter >= 0 ) {
console.log(`The counter is: ${counter}`)
counter++;
}
Sorry, I don't understand the last bit. Can someone please break it down?
This is how I'm interpreting:
- Counter = 0
- while counter less or equal to 0
- log
- Increase 0 +1
- Next loop
Mod edit: added markdown for code readability. Check out the "markdown cheatsheet" linked below the comment box for syntax examples.
1 Answer
Cameron Childres
11,820 PointsHi Cristina,
Your condition as written checks if the counter is greater than or equal to zero. As the counter increases the condition will always be valid so it will continue on forever. There's also a semicolon missing after console.log().
If you change it to <= or = then the loop will run just one time:
let counter = 0;
while ( counter <= 0 ) {
console.log(`The counter is: ${counter}`);
counter++;
}
// logs "The counter is: 0"