I'm trying to iterate through an array of elements. jQuery's documentation says:
Returning non-false is the same as a continue statement in a for loop, it will skip immediately to the next iteration.
I've tried calling 'return non-false;' and 'non-false;' (sans return) neither of which skip to the next iteration. Instead, they break the loop. What am i missing?
Dont forget that you can sometimes just fall off the end of the block to get to the next iteration:
Rather than actually returning like this:
By 'return non-false', they mean to return any value which would not work out to boolean false. So you could return
true
,1
,'non-false'
, or whatever else you can think up.The loop only breaks if you return literally
false
. Ex:This means you can return anything else, including
undefined
, which is what you return if you return nothing, so you can simply use an empty return statement:It's possible this might vary by version; this is applicable for jquery 1.12.4. But really, when you exit out the bottom of the function, you are also returning nothing, and that's why the loop continues, so I would expect that there is no possibility whatsoever that returning nothing could not continue the loop. Unless they want to force everyone to start returning something to keep the loop going, returning nothing has to be a way to keep it going.
Javascript sort of has the idea of 'truthiness' and 'falsiness'. If a variable has a value then, generally 9as you will see) it has 'truthiness' - null, or no value tends to 'falsiness'. The snippets below might help:
Hopefully that helps?
Also, its worth checking out these videos from Douglas Crockford
update: thanks @cphpython for spotting the broken links - I've updated to point at working versions now
The Javascript language
Javascript - The Good Parts
What they mean by non-false is:
So this code:
Will alert one, two, four, five
jQuery.noop() can help