Saturday, January 31, 2026
HomeLanguagesJavascriptJavaScript SyntaxError – A declaration in the head of a for-of loop...

JavaScript SyntaxError – A declaration in the head of a for-of loop can’t have an initializer

This JavaScript exception a declaration in the head of a for-of loop can’t have an initializer occurs if the for -of loop contains an initialization expression like |for (var i = 0 of iterable)|. This is not valid initialization in for-of loops.

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)

Error Type:

SyntaxError

Cause of Error: The loop contains an initialization that is invalid inside for-of loops.

Example 1:

HTML




<!DOCTYPE html>
<html>
<head>
    <title>Syntax Error</title>
</head>
<body>
    <script>
        let iter = [10, 20, 30]; 
        for (let val of iter) { 
          document.write(val + "<br>"); 
        
    </script>
</body>
</html>


Output:

10
20
30

Example 2: This example contains an invalid initialization inside the head of for-of the loop.

HTML




<!DOCTYPE html>
<html>
<head>
    <title>Syntax Error</title>
</head>
<body>
    <script>
        let iter = [10, 20, 30]; 
        for (let val = 10 of iter) { 
              document.write(val + "<br>"); 
        
    </script>
</body>
</html>


Output(in console):

SyntaxError: for-of loop variable declaration may not have 
an initializer.
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32478 POSTS0 COMMENTS
Milvus
122 POSTS0 COMMENTS
Nango Kala
6849 POSTS0 COMMENTS
Nicole Veronica
11978 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12066 POSTS0 COMMENTS
Shaida Kate Naidoo
6987 POSTS0 COMMENTS
Ted Musemwa
7222 POSTS0 COMMENTS
Thapelo Manthata
6934 POSTS0 COMMENTS
Umr Jansen
6917 POSTS0 COMMENTS