-3

While exiting from each loop- return 0 is not working. However, changing it to return false works perfectly.

Fiddle here

The regular JavaScript loops (while,for) exits fine by return 0. Doesn't it breaks the uniformity!

Avisek Chakraborty
  • 8,229
  • 10
  • 48
  • 76
  • 1
    @JohnGreen How does that have anything to do with this question? – JLRishe Apr 16 '15 at 10:06
  • Isn't obvious what the difference is? One is a number the other a boolean to start with. An 0 is not false. – GillesC Apr 16 '15 at 10:07
  • @Vikrant - There's nothing wrong with not understanding the issue. And yes, return 0 does mean exit a loop. Return *anything* means exit a loop. $.each is not a loop, per se. – John Green Apr 16 '15 at 10:08
  • @JLRishe - The issue is that Jquery is doing an identity check to cancel the $.each. It is very specifically looking for a return value that is === false. Since the return value is === 0, it isn't cancelling. This confuses many programmers, since 0 == false. – John Green Apr 16 '15 at 10:10
  • 2
    @JohnGreen Ok, I see your point, but this question isn't remotely a _duplicate_ of that one. – JLRishe Apr 16 '15 at 10:11
  • @JLRishe - You're right, although that is the issue, there is another level of indrection going on there. – John Green Apr 16 '15 at 10:13
  • 1
    @Vikrant, you dint get my point. Sorry that the posted "question text" was so generic. basically the cofusion was- if For & While loops exits/breaks by return 0, why not jQUery "each" !! Doesn't it break the uniformity? – Avisek Chakraborty Apr 16 '15 at 10:35
  • okay @AviC, I got your point and working on it – Vikrant Apr 16 '15 at 10:38
  • @AviC - The issue is intractable. Since jQuery's each runs each iteration through a closure, loop statements (break, continue) will never work. All 'return false' does is simulate a break statement, whereas return [anything else] simulates a continue statement. This is just the nature of how jQuery has to work within the tools provided by javascript, at least without major tradeoffs in functionality and performance. The real key is to never try to think of $.each as anything like a for statement.... they're just different things. – John Green Apr 16 '15 at 11:09

5 Answers5

4

jQuery documentation

We can break the $.each() loop at a particular iteration by making the callback function return false. Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration.

JSFiddle as example

Henrik Andersson
  • 45,354
  • 16
  • 98
  • 92
JLRishe
  • 99,490
  • 19
  • 131
  • 169
1

The difference is simply that 0 is not the exact same thing as false.

Usually when a boolean value is required any value will be usable, and it will be converted to a boolean. In that case the 0 would be converted to false.

In a jQuery $.each loop that wouldn't work. If you don't specifically return anything from a function, then the return value is undefined. If that would be converted to a boolean that would also become false.

The $.each method doesn't convert the return value to a boolean, it specifically looks for the value false. Any other value will let the loop continue.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
1

Simply put, 0 !== false.

In your own code, you may sometimes check for a false-like answer:

0 == false;  // true
null == false;  // true.

However, jQuery (rightly so) uses a strict equality (===) operator.

As such:

0 === false; // false
null === false;  // false
false === false; // true

This if fundamentally an identity vs equality issue: Which equals operator (== vs ===) should be used in JavaScript comparisons?

Community
  • 1
  • 1
John Green
  • 13,241
  • 3
  • 29
  • 51
0

To break a $.each loop, you have to return false in the loop callback.

Returning anything else skips to the next iteration, equivalent to a continue in a normal loop.

Refer Document

Vikrant
  • 4,920
  • 17
  • 48
  • 72
-1

Using return 0, will return zero, which is a number. Using return false says that there is nothing to return, or don't return.

Harigovind R
  • 816
  • 8
  • 17
  • Using `return false;` doesn't say that there is nothing to return, it returns the value `false`. You might think of `return;` which will return without setting a specific return value, leaving it as `undefined`. – Guffa Apr 16 '15 at 10:11
  • `return false` means straight away stop the operation in function and Exit – Vikrant Apr 16 '15 at 10:12
  • @Vikrant buddy `return` means so, not `return false` . "false" is the value- that the function will return. – Avisek Chakraborty Apr 16 '15 at 10:49