All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Break For Loop In JavaScript

Last Updated : Mar 11, 2024

How To Break For Loop In JavaScript

In this tutorial we will show you the solution of how to break for loop in JavaScript, here we are going to show you some example with break statement and we needs to define for loop.

As we know for loop is used for iterate some amount of values and there specified one condition when it meets condition true there we used break statement so it will comes out from the loop suddenly and at last we printed result on console.

Step By Step Guide On How To Break For Loop In JavaScript :-

Here we defined empty array variable ‘txt’ for store iterated values in loop then we defined for loop.

In loop we iterating something until variable i become ‘4’ and within that we specified condition ‘i==3’ when i become ‘3’ then we break will executed and for loop will stopped its iteration.

Until i becomes ‘3’ we storing i integer value to array ‘txt’. After meet break statement at outside for loop we printed array ‘txt’ value to console.

<!DOCTYPE html>
<html>
    <head>
        <title>BREAK FOR LOOP</title>
    </head>
    <body>
        <script>
            let txt=[];
            for(var i=0;i<5;i++){
                if(i==3) break;
                txt+=i+" ";
            }
            console.log(txt);
        </script>
    </body>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  5. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  6. In script we defined variable empty array ‘txt’ for stores iterating values. Then we defined for loop there we defined variable ‘i’.
  7. As we know for loop had three statements, which is first statement defines initial position of for loop, second will be some condition specified, third will be increment or decrement statement.
  8. In for loop we sets i initial position to value ‘0’, condition is ‘i<5’, then increment i by 1 for each iteration.
  9. Within loop we specified another condition ‘i==3’ which is defines when i value become ‘3’ then it will proceeds break statement so this for loop gets stopped.
  10. Otherwise we storing i value each time to the array ‘txt’. When it comes out for loop at that time we printing array ‘txt’ value on console.
  11. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to use break statement in javascript.

When we executes program on browser we can’t see the result because we printed on console panel so we have to use shortcut ‘ctrl+shift+j’ then console panel will open at right hand side with result ‘0 1 2’.

If your need to do some other process within loop you can modify accordingly.

I hope this tutorial on how to break for loop in JavaScript helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪