4

I was reading through this question, I am not able to grasp the concept used for the 'for loop'

Generally, syntax of for loop is for(assign value, check condition, increment){} They have used the for loop but there is no condition checking, how does this work?

for(var i = arr1.length; i--;) {
    if(arr1[i] !== arr2[i])
        return false;
}
Harsh Patel
  • 6,334
  • 10
  • 40
  • 73
Deepika Rao
  • 145
  • 1
  • 3
  • 11
  • Above code is checking if any 2 values are different, the array is not identical and breaks the loop using `return false` – Rajesh Sep 12 '17 at 06:12
  • because there is explicitly condition checking inside the for loop. If the condition gets false then it will return false. – Harsh Patel Sep 12 '17 at 06:13
  • you could use array function like `Array#filter` it's helpful to find the match – prasanth Sep 12 '17 at 06:14
  • @prasad No. Filter will loop till end. This is the best solution – Rajesh Sep 12 '17 at 06:14
  • What I meant was, is it okay to use for loop without the second condtion - should we not write - for(var i = arr1.length;" leave this blank";i--;){} – Deepika Rao Sep 12 '17 at 06:15

3 Answers3

18

Actually, it's

for ([initialization]; [condition]; [final-expression])

where all three expressions are optional.

In this case, i-- is a condition, when it reaches 0 it's falsy, and the loop stops.
The "final-expression" is the one not used here.

var arr = [1, 2, 3, 4];

for (var i = arr.length; i--;) {
  console.log(arr[i]);
}

The for statement sets the value of i to a positive integer, then on each iteration it evaluates the condition, effectively decrementing i until it reaches a number that is falsy, which happens when i reaches zero.


Here's some other examples of how to skip expressions in a for loop

Skipping the initialization

var i = 0;
for (; i < 4; i++) {
    console.log(i);
}

Skipping the condition

for (var i = 0;; i++) {
   console.log(i);
   if (i > 3) break;
}

Skipping everything

var i = 0;

for (;;) {
  if (i > 4) break;
  console.log(i);
  i++;
}
adeneo
  • 312,895
  • 29
  • 395
  • 388
  • @DeepikaRao They're all technically optional depending on what you want to do. `for (;;);` will compile, but probably won't be something you want – flakes Sep 12 '17 at 06:25
0

See the code carefully

for(var i = arr1.length; i--;) {
    if(arr1[i] !== arr2[i])
        return false;
}

Whenever the browser encounters the return statement the control is moved outside the for loop and even outside the function containing this for loop. So, the loops breaks without having a condition when the condition arr1[i] !== arr2[i] is satisfied.

Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
0

All the three arguments in a for loop is optional.

Here the loop iterates with i value changing from arr1.length -> 1. Decrementing i is done in the second argument itself. Since the second argument is for condition checking it will return a falsy value when i becomes zero, and the iteration stops. If the arrays don't match the loop will return false in the middle.