SyntaxError: a declaration in the head of a for-of loop can't have an initializer
The JavaScript exception "a declaration in the head of a for-of loop can't have an initializer" occurs when the head of afor...of loop contains an initializer expression such asfor (const i = 0 of iterable). This is not allowed in for-of loops.
In this article
Message
SyntaxError: for-of loop variable declaration may not have an initializer. (V8-based)SyntaxError: a declaration in the head of a for-of loop can't have an initializer (Firefox)SyntaxError: Cannot assign to the loop variable inside a for-of loop header. (Safari)
Error type
SyntaxErrorWhat went wrong?
The head of afor...of loop contains an initializer expression. That is, a variable is declared and assigned a valuefor (const i = 0 of iterable). This is not allowed in for-of loops. You might want afor loop that does allow an initializer.
Examples
>Invalid for-of loop
js
const iterable = [10, 20, 30];for (const value = 50 of iterable) { console.log(value);}// SyntaxError: a declaration in the head of a for-of loop can't// have an initializerValid for-of loop
You need to remove the initializer (value = 50) in the head of thefor-of loop. Maybe you intended to make 50 an offset value, in that case you could add it to the loop body, for example.
js
const iterable = [10, 20, 30];for (let value of iterable) { value += 50; console.log(value);}// 60// 70// 80