while
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
while
문은 조건문이 참일 때 실행되는 반복문이다. 조건은 문장안이 실행되기 전에 참, 거짓을 판단한다.
문법
예제
다음의 while문은 n이 3보다 작을 때까지 반복한다.
js
var n = 0;
var x = 0;
while (n < 3) {
n++;
x += n;
}
반복을 살펴보면, n을 x에 계속 더하게 된다. 그러므로 x와 n 변수는 다음의 값을 갖는다.
- 첫번째 반복; n=1 과 x=1
- 두번째 반복; n=2 과 x=3
- 세번째 반복; n=3 과 x=6
세번째 반복후, n<3 이라는 초건은 더 이상 참이아니가 되므로 반복은 종료된다
명세
Specification |
---|
ECMAScript® 2025 Language Specification # sec-while-statement |
브라우저 호환성
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
while |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.