자바스크립트에서 가장 기본적인 루프는 while 루프입니다. while 루프의 목적은 표현식이 참인 한 명령문이나 코드 블록을 반복적으로 실행하는 것입니다. 표현식이 거짓이 되면 루프가 종료됩니다.
구문
자바스크립트에서 while 루프의 구문은 다음과 같습니다 -
while (expression){
Statement(s) to be executed if expression is true
} 예
다음 예제를 실행하여 while 루프를 구현할 수 있습니다.
라이브 데모
<html>
<body>
<script>
var count = 0;
document.write("Starting Loop ");
while(count < 10){
document.write("Current Count : " + count + "<br/>");
count++;
}
document.write("Loop stopped!");
</script>
</body>
</html>