JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops For Loops

Iosif Skorohodov
Iosif Skorohodov
1,123 Points

What is the ++ all about?

I only got this correct because I had to Google search for the answer.

for (var num = 4; num <= 156; num++){ console.log(num); } How is this the correct answer? What does the ++ all about? This was my code:

for (var num = 4; num = 156){ console.log(num); }

1 Answer

okay. the ++ is a shorthand method. The long method would be num = num +1;. Each time you run thorough the for loop you take the existing value for num, and add 1 to it. It's so common that we use num++ instead of the full statement. There are a few other shorthands that do basically the same and you may come across and look up as you encounter them, but for now just know it's the shorthand for getting the current value then adding 1 to it. Hope that helps!

In your code their you are setting, the start value, the end value, but are missing the increment value, which is what you need to count through the number of loops.