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

while - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

while

20157

while

let n = 0;

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

console.log(n);
// Expected output: 3

js
while (condition)
  statement
condition

statement while

statement

{ /* ... */ }

break condition

n while

js
let n = 0;
let x = 0;

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

n n x x n

  • n = 1x = 1
  • n = 2x = 3
  • n = 3x = 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()
  3. console.log()

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

=== = = ===

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());
}

ECMAScript 2027 LanguageSpecification
# sec-while-statement


Web Proxy Viewer  |  New URL  |  Original Page