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

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

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 . (Symbol .)

In this article

const object = { a: 1, b: 2, c: 3 };

for (const property in object) {
  console.log(`${property}: ${object[property]}`);
}

// Expected output:
// "a: 1"
// "b: 2"
// "c: 3"

js
for (const variable in object) {
  statement;
}

variable

(Value name) (variable) .

object

.

for...in non-Symbol .

Array Object constructor String indexOf(), Object toString() Object.prototype, String.prototype . for...in . ( .)

, ,

for..in . ( cross-browser delete .)

, . . .

, , . , ( ) , .

for...in

Note: for...in Array .

, . for...in . for...in , .

, . for .( Array.prototype.forEach(), for...of .)

getOwnPropertyNames() hasOwnProperty() .(propertyIsEnumerable() .) .

for...in ?

for...in , , Array.prototype.forEach(), for...of . for...in ?

( ) . , - ( "key" ) for...in .

for...in

non-Symbol .

js
var obj = { a: 1, b: 2, c: 3 };

for (const prop in obj) {
  console.log(`obj.${prop} = ${obj[prop]}`);
}

// Output:
// "obj.a = 1"
// "obj.b = 2"
// "obj.c = 3"

hasOwnProperty() . .

js
var triangle = { a: 1, b: 2, c: 3 };

function ColoredTriangle() {
  this.color = "red";
}

ColoredTriangle.prototype = triangle;

function show_own_props(obj, objName) {
  var result = "";

  for (var prop in obj) {
    if (obj.hasOwnProperty(prop)) {
      result += objName + "." + prop + " = " + obj[prop] + "\n";
    }
  }

  return result;
}

o = new ColoredTriangle();
alert(show_own_props(o, "o")); /* alerts: o.color = red */

Specification
ECMAScript 2027 LanguageSpecification
# sec-for-in-and-for-of-statements

Compatibility: Initializer expressions in strict mode

Prior to Firefox 40, it was possible to use an initializer expression (i=0) in a for...in loop:

js
const obj = { a: 1, b: 2, c: 3 };
for (var i = 0 in obj) {
  console.log(obj[i]);
}
// 1
// 2
// 3

This nonstandard behavior is now ignored in version 40 and later, and will present a SyntaxError ("for-in loop head declarations may not have initializers") error in strict mode (bug 748550 and bug 1164741).

Other engines such as v8 (Chrome), Chakra (IE/Edge), and JSC (WebKit/Safari) are investigating whether to remove the nonstandard behavior as well.

See also


Web Proxy Viewer  |  New URL  |  Original Page