blog

Home / DeveloperSection / Blogs / Using JavaScript Nested Loops

Using JavaScript Nested Loops

Danish Khan 23521 09-Nov-2012

Introduction

In this blog I am going to explain the use of nested for loops in JavaScript.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Nested loops in JavaScript</title>
</head>
<body>
    <script type="text/javascript">
        var i, j, mulTot;
        for (i = 1; i <= 15; i++) {
            for (j = 1; j < 15; j++) {
                mulTot = i * j;
 
                if (mulTot < 10) {
                    mulTot = " " + mulTot;
                }
                else {
                    mulTot = " " + mulTot;
                }
                document.write(mulTot);
            }
            document.write("<BR>");
        }
    </script>
</body>
</html>

Output:

Using JavaScript Nested Loops

Nested loops are used in most of the places it is used in Matrix multiplication,  Displaying tables and many other places. In this example first the outer loop is initialized and if the condition  is true it will give control to the inner loop and this inner loop will keep on incrementing till the condition of the inner loop is true and when the condition of the inner loop becomes false then only the control will be transferred to the outer loop.

Conclusion:

Through this blog we came to know about how to use multiple loops in our program.


Updated 18-Sep-2014

Leave Comment

Comments

Liked By