Get to know MDN better
The JavaScript strict mode-only exception "for-in loop head declarations may not have initializers" occurs when the head of a for...in contains an initializer expression, such as for (var i = 0 in obj). This is not allowed in for-in loops in strict mode. In addition, lexical declarations with initializers like for (const i = 0 in obj) are not allowed outside strict mode either.
The head of a for...in loop contains an initializer expression. That is, a variable is declared and assigned a value for (var i = 0 in obj). In non-strict mode, this head declaration is silently ignored and behaves like for (var i in obj). In strict mode, however, a SyntaxError is thrown. In addition, lexical declarations with initializers like for (const i = 0 in obj) are not allowed outside strict mode either, and will always produce a SyntaxError.
This example throws a SyntaxError:
You can remove the initializer (i = 0) in the head of the for-in loop.
The for...in loop shouldn't be used for Array iteration. Did you intend to use a for loop instead of a for-in loop to iterate an Array? The for loop allows you to set an initializer then as well:
This page was last modified on Jul 8, 2025 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.