एक ब्लॉक स्टेटमेंट शून्य या अधिक स्टेटमेंट को ग्रुप करता है। जावास्क्रिप्ट के अलावा अन्य भाषाओं में, इसे यौगिक कथन के रूप में जाना जाता है।
सिंटैक्स
यहां सिंटैक्स है -
{
//List of statements
} एक ब्लॉक में एक लेबल जोड़ने के लिए, निम्नलिखित का उपयोग करें -
Identifier_for_label: {
StatementList
} चलो इसे ब्रेक स्टेटमेंट के लिए उपयोग करते हैं। आप ब्रेक स्टेटमेंट के साथ प्रवाह को नियंत्रित करने के लिए लेबल का उपयोग करने के लिए निम्न कोड चलाने का प्रयास कर सकते हैं
उदाहरण
लाइव डेमो
<html>
<body>
<script>
document.write("Entering the loop!<br /> ");
outerloop: // This is the label name
for (var i = 0; i < 5; i++) {
document.write("Outerloop: " + i + "<br />");
innerloop:
for (var j = 0; j < 5; j++) {
if (j > 3 ) break ; // Quit the innermost loop
if (i == 2) break innerloop; // Do the same thing
if (i == 4) break outerloop; // Quit the outer loop
document.write("Innerloop: " + j + " <br />");
}
}
document.write("Exiting the loop!<br /> ");
</script>
</body>
</html>