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,87810 gold badges44 silver badges77 bronze badges
asked Sep 12, 2017 at 6:11
5
  • Above code is checking if any 2 values are different, the array is not identical and breaks the loop using return false Commented Sep 12, 2017 at 6:12
  • because there is explicitly condition checking inside the for loop. If the condition gets false then it will return false. Commented Sep 12, 2017 at 6:13
  • you could use array function like Array#filter it's helpful to find the match Commented Sep 12, 2017 at 6:14
  • @prasad No. Filter will loop till end. This is the best solution Commented Sep 12, 2017 at 6: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--;){} Commented Sep 12, 2017 at 6:15

3 Answers 3

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++;
}

answered Sep 12, 2017 at 6:14
Sign up to request clarification or add additional context in comments.

1 Comment

@DeepikaRao They're all technically optional depending on what you want to do. for (;;); will compile, but probably won't be something you want
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.

answered Sep 12, 2017 at 6:14

Comments

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.

answered Sep 12, 2017 at 6:23

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.