‘break’ and ‘continue’ for jQuery loop

Try jQuery: Ron highly recommends this course.
Try jQuery: Ron highly recommends this course. Its free!

jQuery functions like $.each( Object obj, Function fn ) or $().each() are not loop rather these are iterative functions. $().each() is used to iterate over a jQuery object and you can use $.each( Object obj, Function fn ) to iterate over anything (for example, an array). As they iterate over something we call them loop.

So jQuery loops are not like javascript loops.

We use `break;` and `continue;` inside a loop. These will not work inside a jQuery loop (like $.each()). So what you will do, if you need the same functionality for a jQuery loop. Fortuitously there is another way. You can use `return false;` inside a jQuery loop to break it. The idea is simple. jQuery loops are functions. So when you `return false;` from a function, it will immediately stop execution of that function and will not continue. Which will work as `break;`. Similarly if you write `return;` (without an argument) it will work as `continue`;

Here are two examples:-


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.