--- title: while slug: Web/JavaScript/Reference/Statements/while tags: - 반복문 - 자바스크립트 translation_of: Web/JavaScript/Reference/Statements/while ---
while문은 조건문이 참일 때 실행되는 반복문이다. 조건은 문장안이 실행되기 전에 참, 거짓을 판단한다.
while (condition) statement
조건문장다음의 while문은 n이 3보다 작을 때까지 반복한다.
var n = 0;
var x = 0;
while (n < 3) {
n++;
x += n;
}
반복을 살펴보면, n을 x에 계속 더하게 된다. 그러므로 x와 n 변수는 다음의 값을 갖는다.
세번째 반복후, n<3 이라는 초건은 더 이상 참이아니가 되므로 반복은 종료된다
| Specification | Status | Comment |
|---|---|---|
| {{SpecName('ESDraft', '#sec-while-statement', 'while statement')}} | {{Spec2('ESDraft')}} | |
| {{SpecName('ES6', '#sec-while-statement', 'while statement')}} | {{Spec2('ES6')}} | |
| {{SpecName('ES5.1', '#sec-12.6.2', 'while statement')}} | {{Spec2('ES5.1')}} | |
| {{SpecName('ES3', '#sec-12.6.2', 'while statement')}} | {{Spec2('ES3')}} | |
| {{SpecName('ES1', '#sec-12.6.1', 'while statement')}} | {{Spec2('ES1')}} | Initial definition |
{{CompatibilityTable}}
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
do...while