JavaScript Tutorial For Beginners #18 – Break & Continue




Break and Continue are two reserved keywords in JavaScript that help us when trying to ‘get out’ of a loop, and we’ll cover both in this tutorial.

Break, as the name suggests, breaks out of a loop at any given point, and Continue just tells the loop to ‘skip’ this particular iteration and carries on with the next one.

As always, if you have any questions, ask away :).

SUBSCRIBE TO CHANNEL – https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg?sub_confirmation=1

========== JavaScript for Beginners Playlist ==========

========== CSS for Beginners Playlist ==========

========== HTML for Beginners Playlist ==========

========== The Net Ninja ============

For more front-end development tutorials & to black-belt your coding skills, head over to – https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg or http://thenetninja.co.uk

========== Social Links ==========

Twitter – @TheNetNinja – https://twitter.com/thenetninjauk

Original source


22 responses to “JavaScript Tutorial For Beginners #18 – Break & Continue”

  1. I used continue in my loop and Airbnb eslint told me I'm not allowed to use it. Went on the Internet and read that it's bad for readability of your code and you shouldn't use it.
    How would you replace it? Is there a trick that doesn't make your code less readable and skips one iteration or should you write your code in a totally different way from the start with this in mind to omit "continue" usage?

  2. I need some help in building break and continue javascript
    I want to write a code where there is like 5 switch toggles
    If toggle1 is ON, audio1 will be played, then continue to toggle2
    if toggle2 is OFF, break and skip to toggle3
    if toggle3 is ON, audio3 will be played

    until toggle5
    How can i write such code, please help

  3. It's easy to understand all these Javascript rules but hard to understand the practical use of all these codes in web development. it would be so kind of you if you explain the actual use of these codes at the same time.

  4. If anyone is still confused by what "continue" does, copy and execute this code in console and it will be clearer.

    var myLoop = document.getElementsByTagName("p");

    for (i = 1; i <= myLoop.length; i++) {

    if (i === 5 || i === 3) {
    console.log("Restart loop when equal " + i);
    continue;
    }
    console.log("this is number " + i);

    if(i === 7) {
    break;
    }
    }
    console.log("loop ended successfully");

  5. Hey mate, quick question for "while, for, break & continue" – can you give an example on where can I possibly use this codes on a certain project? I.e. how or why would I use this when creating a website or something else. JS is daunting seriously. Lol.

Leave a Reply