คำสั่ง break ใช้เพื่อออกจากลูปก่อนกำหนด โดยแยกออกจากวงเล็บปีกกาที่ปิดไว้
ตัวอย่าง
คุณสามารถลองเรียกใช้โค้ดต่อไปนี้เพื่อเรียนรู้วิธีใช้คำสั่ง break เพื่อออกจากลูป
การสาธิตสด
<html>
<body>
<script>
var x = 1;
document.write("Entering the loop<br /> ");
while (x < 20) {
if (x == 5) {
break; // breaks out of loop completely
}
x = x + 1;
document.write( x + "<br />");
}
document.write("Exiting the loop!<br /> ");
</script>
</body>
</html>