Web Development

JavaScript Branching Statements

JavaScript Branching Statement is section 17 of Beginner’s Guide to JavaScript.

In JS we are talking about two types of branching statements.

  • The break statement “jumps out” of a loop.
  • The continue statement “jumps over” one iteration in the loop.

In the previous article we introduced break statement but didn’t go into much detail. So let’s talk about them with some examples.

The Break Statement

We used break statements in a switch statement on the previous article. It was used to “jump out” from the switch. The break statement can also be used to jump out of a loop.  The break statement breaks the loop and continues executing the code after the loop if there any.

[code languague=”js”] for (i = 0; i < 10; i++) { if (i == 3) { break; } text += "The number is " + i + "
“;
}
[/code]

In the above code segment only upto number 2 will be printed. because at the number 3, if condition will be satisfied and entered into executing the break statement and jump out from the loop.

(Loop statements are discussed more in detail from another section)

The Continue Statement

This mostly used in loops. The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.

[code languague=”js”] for (i = 0; i < 10; i++) { if (i == 3) { continue; } text += "The number is " + i + "
“;
}
[/code]

This example skips the value of 3 and print all the others.

Using JavaScript Labels in Branching

JavaScript statements can be labeled. To label JavaScript statements you precede the statements with a label name and a colon’:’.

label:{

statements

}

 

This comes useful when you want to jump out or over from a specific labelled statement.

[code languague=”js”] myForStmt:{
for (i = 0; i < 10; i++) { myIfStmt:{ if (i == 3) { break myIfStmt; } } text += "The number is " + i + "
“;
}
}
[/code]

We labelled the for statement and the if statement. So here we are not breaking from the default for statement. Rather than that we are breaking from a specific label, which is the if statement. Here is another example.

[code languague=”js”] cars = [“BMW”, “Volvo”, “Saab”, “Ford”];
text = “”;

list: {
text += cars[0] + “
“;
text += cars[1] + “
“;
text += cars[2] + “
“;
break list;
text += cars[3] + “
“;
text += cars[4] + “
“;
text += cars[5] + “
“;
}

document.getElementById(“demo”).innerHTML = text;
[/code]

This is a labelled break without a loop or a switch. But for a continue statement with or without a label reference can only be used inside a loop.


 

What’s Next?

Learn what is JavaScript Looping

Join Our Newsletter Today

Stay updated with all latest updates.
Give it a try, you can unsubscribe anytime.
Subscribe
SUBSCRIBE NOW

About the author

Sandeep Likhar

Sandeep Likhar from Indore India, Blogger, eBook Designer and Founder of LetsDnD. Having five years of experience in industry as a Digital Publishing Expert and eBook Converter providing services to Authors, Publishers and distributors worldwide. Proficient in converting books into various formats HTML, epub, mobi, word, PDF including all major online platforms iTunes, Kobo, Kindle, Createspace, B&N, Smashwords and more.

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz
DON’T MISS OUT!
Subscribe To Newsletter
Be the first to get latest updates and exclusive content straight to your email inbox.
Stay Updated
Give it a try, you can unsubscribe anytime.

Join Our Newsletter Today

Stay updated with all latest updates.
Give it a try, you can unsubscribe anytime.
Subscribe
SUBSCRIBE NOW