SyntaxError: a declaration in the head of a for-of loop can't have an initializer
Message
SyntaxError: for-of loop head declarations cannot have an initializer (Edge) SyntaxError: a declaration in the head of a for-of loop can't have an initializer (Firefox) SyntaxError: for-of loop variable declaration may not have an initializer. (Chrome)
Type d'erreur
Quel est le problème ?
Exemples
Boucles for-of
invalides
js
let iterable = [10, 20, 30]; for (let value = 50 of iterable) { console.log(value); } // SyntaxError: a declaration in the head of a for-of loop can't // have an initializer
Boucles for-of
valides
Il faut retirer l'initialisateur de l'en-tête de la boucle pour ne plus avoir l'erreur. Si cette valeur devait servir d'incrément, on peut ajouter l'addition dans le corps de la boucle.
js
let iterable = [10, 20, 30]; for (let value of iterable) { value += 50; console.log(value); } // 60 // 70 // 80
Voir aussi
for...of
for...in
interdit également d'utiliser un initialisateur en mode strict (SyntaxError: for-in loop head declarations may not have initializers)for
permet de définir un initialisateur lors de l'itération