Message
Warning: JavaScript 1.6'sfor-each-in
loops are deprecated; consider using ES6for-of
instead
Error Type
Warning
What went wrong?
JavaScript 1.6's {{jsxref("Statements/for_each...in", "for each (variable in obj)")}} statement is deprecated, and will be removed near future.
Examples
Iterating over an object
{{jsxref("Statements/for_each...in", "for each...in")}} iterates over the specified object's values.
Deprecated syntax
var object = { a: 10, b: 20 }; for each (var x in object) { console.log(x); // 10 // 20 }
Alternative standard syntax
You can use {{jsxref("Statements/for...in", "for...in")}} to iterate over the specified object's keys, and get each value inside loop.
var object = { a: 10, b: 20 }; for (var key in object) { var x = object[key]; console.log(x); // 10 // 20 }
With ES6 and experimental feature, you can use {{jsxref("Object.values")}} to get an array with the specified object's values, and iterate over the array with {{jsxref("Statements/for...of", "for...of")}}.
var object = { a: 10, b: 20 }; for (var x of Object.values(object)) { console.log(x); // 10 // 20 }
Iterating over an array
{{jsxref("Statements/for_each...in", "for each...in")}} iterates over the specified array's elements.
Deprecated syntax
var array = [10, 20, 30]; for each (var x in array) { console.log(x); // 10 // 20 // 30 }
Alternative standard syntax
With ES6 feature, you can iterate over the specified array's elements with {{jsxref("Statements/for...of", "for...of")}}.
var array = [10, 20, 30]; for (var x of array) { console.log(x); // 10 // 20 // 30 }
Iterating over an array null or undefined
{{jsxref("Statements/for_each...in", "for each...in")}} does nothing if specified value is null
or undefined
, but {{jsxref("Statements/for...of", "for...of")}} throws exception in those cases.
Deprecated syntax
function func(array) { for each (var x in array) { console.log(x); } } func([10, 20]); // 10 // 20 func(null); // print nothing func(undefined); // print nothing
Alternative standard syntax
To rewrite {{jsxref("Statements/for_each...in", "for each...in")}} that value can be null
or undefined
with {{jsxref("Statements/for...of", "for...of")}}, you need a guard around {{jsxref("Statements/for...of", "for...of")}}.
function func(array) { if (array) { for (var x of array) { console.log(x); } } } func([10, 20]); // 10 // 20 func(null); // print nothing func(undefined); // print nothing
Iterating over an object's key-value pair
Deprecated syntax
There's a deprecated idiom to iterate over the specified object's key-value pairs by {{jsxref("Statements/for_each...in", "for each...in")}} and {{jsxref("Global_Objects/Iterator", "Iterator")}}.
var object = { a: 10, b: 20 }; for each (var [key, value] in Iterator(object)) { console.log(key, value); // "a", 10 // "b", 20 }
Alternative standard syntax
You can use {{jsxref("Statements/for...in", "for...in")}} to iterate over the specified object's keys, and get each value inside loop.
var object = { a: 10, b: 20 }; for (var key in object) { var value = object[key]; console.log(key, value); // "a", 10 // "b", 20 }
With ES6 and experimental feature, you can use {{jsxref("Object.entries")}} to get an array with the specified object's key-value pair, and iterate over the array with {{jsxref("Statements/for...of", "for...of")}}.
var object = { a: 10, b: 20 }; for (var [key, value] of Object.entries(object)) { console.log(key, value); // "a", 10 // "b", 20 }
See also
- {{jsxref("Statements/for...of", "for...of")}}
- {{jsxref("Object.values")}}
- {{jsxref("Object.entries")}}