별초롱언니 2025. 4. 3. 14:25

01. 개념

"반복 횟수를 모를 때" 조건식에 의해 반복 여부를 결정하기 위해 사용한다.

while문은 일반 while문과 do while문으로 구분된다.

 

02. 사용법

while (조건식) {

   // 반복할 구문

}

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>while문</title>
</head>
<body>
  <script>
    var i = 0;
    var array01 = ["사과","배","포도","천혜향","레몬","딸기","복숭아","오렌지","자몽"];
    while (i < array01.length) {
      document.write(array01[i] + "<br>");
      i++;
    }
  </script>
</body>
</html>