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

for - 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

for

Baseline Widely available

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

for , ( ) .

In this article

let str = "";

for (let i = 0; i < 9; i++) {
  str = str + i;
}

console.log(str);
// Expected output: "012345678"

js
for ([initialization]; [condition]; [final-expression])
   statement
initialization

( ) . . var let . var . for . let .

.

condition

. statement . . for .

final-expression

. condition . .

statement

. ({ ... }) . (;) .

for

for i 0 . i 9 i 1 .

js
for (var i = 0; i < 9; i++) {
  console.log(i);
  //  
}

for 3 .

, initialization .

js
var i = 0;
for (; i < 9; i++) {
  console.log(i);
  //  
}

initialization condition . , .

js
for (var i = 0; ; i++) {
  console.log(i);
  if (i > 3) break;
  //  
}

. break , .

js
var i = 0;

for (;;) {
  if (i > 3) break;
  console.log(i);
  i++;
}

for

for final-expression .

js
function showOffsetPos(sId) {
  var nLeft = 0,
    nTop = 0;

  for (
    var oItNode = document.getElementById(sId) /* initialization */;
    oItNode /* condition */;
    nLeft += oItNode.offsetLeft,
      nTop += oItNode.offsetTop,
      oItNode = oItNode.offsetParent /* final-expression */
  ); /* semicolon */

  console.log(
    "Offset position of '" +
      sId +
      "' element:\n left: " +
      nLeft +
      "px;\n top: " +
      nTop +
      "px;",
  );
}

/* Example call: */

showOffsetPos("content");

// Output:
// "Offset position of "content" element:
// left: 0px;
// top: 153px;"

: , JavaScript . .

Specification
ECMAScript 2027 LanguageSpecification
# sec-for-statement


Web Proxy Viewer  |  New URL  |  Original Page