To tłumaczenie jest niepełne. Pomóż przetłumaczyć ten artykuł z języka angielskiego

Wyrażenie iteruje po wszystkich niesymbolicznych, wyliczalnych własnościach obiektu. over all non-Symbol, enumerable properties of an object.


for (zmienna in obiekt)
W każdej iteracji, zmiennej przypisywana jest inna nazwa własności.
Obiekt, po którego niesymbolicznych wyliczalnych własnościach iterujemy.


A loop only iterates over enumerable, non-Symbol properties. Objects created from built–in constructors like Array and Object have inherited non–enumerable properties from Object.prototype and String.prototype, such as String's indexOf() method or Object's toString() method. The loop will iterate over all enumerable properties of the object itself and those the object inherits from its constructor's prototype (properties closer to the object in the prototype chain override prototypes' properties).

Usunięte, dodane lub zmodyfikowane własności

A loop iterates over the properties of an object in an arbitrary order (see the delete operator for more on why one cannot depend on the seeming orderliness of iteration, at least in a cross-browser setting).

If a property is modified in one iteration and then visited at a later time, its value in the loop is its value at that later time. A property that is deleted before it has been visited will not be visited later. Properties added to the object over which iteration is occurring may either be visited or omitted from iteration.

In general, it is best not to add, modify, or remove properties from the object during iteration, other than the property currently being visited. There is no guarantee whether an added property will be visited, whether a modified property (other than the current one) will be visited before or after it is modified, or whether a deleted property will be visited before it is deleted.

Iterowanie po tablicy i

Uwaga: wyrażenie nie powinno być używane na obiektach klasyArray, tam gdzie porządek indeksu jest ważny.

Array indexes are just enumerable properties with integer names and are otherwise identical to general object properties. There is no guarantee that will return the indexes in any particular order. The loop statement will return all enumerable properties, including those with non–integer names and those that are inherited.

Because the order of iteration is implementation-dependent, iterating over an array may not visit elements in a consistent order. Therefore, it is better to use a for loop with a numeric index (or Array.prototype.forEach() or the for...of loop) when iterating over arrays where the order of access is important.

Iterating over own properties only

If you only want to consider properties attached to the object itself, and not its prototypes, use getOwnPropertyNames() or perform a hasOwnProperty() check (propertyIsEnumerable() can also be used). Alternatively, if you know there won't be any outside code interference, you can extend built-in prototypes with a check method.

Dlaczego używać

Given that is built for iterating object properties, not recommended for use with arrays, and options like Array.prototype.forEach() and for...of exist, what might be the use of at all?

It may be most practically used for debugging purposes, being an easy way to check the properties of an object (by outputting to the console or otherwise). Although arrays are often more practical for storing data, in situations where a key-value pair is preferred for working with data (with properties acting as the "key"), there may be instances where you want to check if any of those keys hold a particular value.


The loop below iterates over all of the object's enumerable, non-Symbol properties and logs a string of the property names and their values.

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"

The following function illustrates the use of hasOwnProperty(): the inherited properties are not displayed.

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

function ColoredTriangle() {
  this.color = 'red';

ColoredTriangle.prototype = triangle;

var obj = new ColoredTriangle();

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

// Output:
// "obj.color = red"


Specyfikacja Status Komentarz
ECMAScript (ECMA-262)
The definition of ' statement' in that specification.
Living Standard
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of ' statement' in that specification.
ECMAScript 5.1 (ECMA-262)
The definition of ' statement' in that specification.
ECMAScript 3rd Edition (ECMA-262)
The definition of ' statement' in that specification.
ECMAScript 1st Edition (ECMA-262)
The definition of ' statement' in that specification.
Standard Definicja początkowa.

Wsparcie przeglądarek

Update compatibility data on GitHub
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
for...inChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 6Opera Full support 2Safari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0nodejs Full support 0.1.100


Full support  
Full support

Compatibility: Initializer expressions in strict mode

Prior to SpiderMonkey 40 (Firefox 40 / Thunderbird 40 / SeaMonkey 2.37), it was possible to use an initializer expression (i=0) in a loop:

var obj = {a: 1, b: 2, c: 3};
for (var i = 0 in obj) {
// 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 (błąd 748550 and błąd 1164741).

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

Zobacz też