[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Statements/while [Back]  [Original]

while - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

while

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 7.

while , . .

In this article

let n = 0;

while (n < 3) {
  n++;
}

console.log(n);
//  : 3

js
while (condition)
  statement
condition

. , statement . , while .

statement

. , .

, statement .

while

while n 3 .

js
let n = 0;
let 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 , .

. , .

, , .

js
const iterator = document.createNodeIterator(document, NodeFilter.SHOW_COMMENT);
let currentNode;
while (currentNode = iterator.nextNode()) {
  console.log(currentNode.textContent.trim());
}

, .

js
while (currentNode = iterator.nextNode()) {

.

  1. iterator.nextNode() , currentNode .
  2. currentNode = iterator.nextNode() truthy.
  3. console.log() .

... .

  1. iterator.nextNode() null .
  2. currentNode = iterator.nextNode() null, .
  3. .

, === = . = === ( ).

, ESLint no-cond-assign , .

Expected a conditional expression and instead saw an assignment. ( )

. , .

js
const iterator = document.createNodeIterator(document, NodeFilter.SHOW_COMMENT);
let currentNode;
while ((currentNode = iterator.nextNode())) {
  console.log(currentNode.textContent.trim());
}

ESLint no-cond-assign Prettier , .

.

js
while ((currentNode = iterator.nextNode()) !== null) {

.

js
while ((currentNode = iterator.nextNode()) && currentNode) {

while .

js
const iterator = document.createNodeIterator(document, NodeFilter.SHOW_COMMENT);
for (
  let currentNode = iterator.nextNode();
  currentNode;
  currentNode = iterator.nextNode()
) {
  console.log(currentNode.textContent.trim());
}

. , .

Specification
ECMAScript 2027 LanguageSpecification
# sec-while-statement


Web Proxy Viewer  |  New URL  |  Original Page