Trying to Learn Javascript Trying to Learn Javascript - 9 months ago 29
Javascript Question

Basic For Loop Logic Doesn't make sense to me

I've been trying to learn Javascript for the longest time, and it is kind of frustrating. I'm back to learning the basics again, and I was playing around with these

loops and arrays below.

Can someone please explain why the output of this code is
and not

var game1 = new Array();
for(i = 25; i > "love is like a river".length; i = i - 1) {


The "love is like a river".length is 20. So the loop is equivalent to this:

for(i = 25; i>20; i=i-1){


That being said, you have 5 calls of the console.log passing to it the following arguments one at each for step:


Not knowing exactly what game1.push does, it's hard to tell. However, supposing that game1 is an array, you would get at each push the new length of the array.

According to MDN:

The push() method adds one or more elements to the end of an array and returns the new length of the array.