วัตถุประสงค์ของลูป while คือการดำเนินการคำสั่งหรือบล็อกโค้ดซ้ำๆ ตราบใดที่นิพจน์เป็นจริง เมื่อนิพจน์กลายเป็นเท็จ การวนซ้ำจะสิ้นสุดลง
ตัวอย่าง
คุณสามารถลองเรียกใช้โค้ดต่อไปนี้เพื่อเรียนรู้วิธีใช้ nested while loop
สาธิตสด
<html>
<body>
<script>
var height = 2;
var width = 8;
var col = 0;
var row = 0;
document.write("Starting Loop<br> ");
while (row < height) {
col = 0;
while(col < width) {
document.write("#");
col++;
}
document.write("<br>");
row++;
}
document.write("Loop stopped!");
</script>
</body>
</html>