| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Statements/for...in | [Back] [Original] |
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 7.
for...in . (Symbol .)
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"
for (const variable in object) {
statement;
}
for...in non-Symbol .
Array Object constructor String indexOf(), Object toString() Object.prototype, String.prototype . for...in . ( .)
for..in . ( cross-browser delete .)
, . . .
, , . , ( ) , .
Note:
for...inArray.
, . for...in . for...in , .
, . for .( Array.prototype.forEach(), for...of .)
getOwnPropertyNames() hasOwnProperty() .(propertyIsEnumerable() .) .
for...in , , Array.prototype.forEach(), for...of . for...in ?
( ) . , - ( "key" ) for...in .
non-Symbol .
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() . .
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 |
Prior to Firefox 40, it was possible to use an initializer expression
(i=0) in a for...in loop:
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.
for...of a similar statement that iterates
over the property valuesforfor...of syntax)Object.getOwnPropertyNames()Object.hasOwn()Array.prototype.forEach()This page was last modified on 2026 7 15 by MDN contributors.
Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |