การวนซ้ำ do...while นั้นคล้ายกับลูป while ยกเว้นว่าการตรวจสอบเงื่อนไขจะเกิดขึ้นที่ส่วนท้ายของลูป ซึ่งหมายความว่าลูปจะดำเนินการอย่างน้อยหนึ่งครั้งเสมอ แม้ว่าเงื่อนไขจะเป็นเท็จ
ไวยากรณ์
ไวยากรณ์สำหรับลูป do-while ใน JavaScript มีดังต่อไปนี้
do{
Statement(s) to be executed;
} while(expression); ตัวอย่าง
ลองใช้ตัวอย่างต่อไปนี้เพื่อเรียนรู้วิธีการใช้งาน do-while loop ใน JavaScript
<html>
<body>
<script>
var count = 0;
document.write("Starting Loop" + "<br />");
do{
document.write("Current Count : " + count + "<br/>");
count++;
}
while(count < 5);
document.write ("Loop stopped!");
</script>
</body>
</html>