Back to Web Development
2026-04-087 min read

SyntaxError: a declaration in the head of a for-of loop can't have an initializer

Learn SyntaxError: a declaration in the head of a for-of loop can't have an initializer step by step with clear examples and exercises.

Title: Mastering JavaScript For-Of Loops: Avoiding Syntax Errors and Common Pitfalls

Why This Matters

In web development, understanding and mastering the intricacies of JavaScript is crucial for writing efficient, error-free code. One such aspect that can trip up even experienced developers is the "SyntaxError: a declaration in the head of a for-of loop can't have an initializer" error. This article will delve into what this error means, why it occurs, and how to avoid it when working with for-of loops.

By understanding this error and its implications, developers can write more robust code that is less prone to errors and easier to maintain. Additionally, familiarity with the rules surrounding for-of loops will help you use their benefits in your web development projects.

Prerequisites

Before diving into the specifics of the for-of loop syntax error, it's essential to have a basic understanding of the following:

  1. JavaScript programming language
  2. Loops in JavaScript (for, while, and for-of)
  3. Variables and data types in JavaScript
  4. Basic HTML and CSS structure
  5. Understanding of objects and arrays in JavaScript
  6. Familiarity with error handling and debugging techniques

What is a For-Of Loop?

A for-of loop is a modern JavaScript looping construct used to iterate over iterable objects such as arrays, strings, maps, and sets. It provides an easier and more readable way to traverse these objects compared to the traditional for and while loops.

Core Concept

The for-of loop works by declaring a loop variable in its header, which is then assigned the value of each iterable object's entry as the loop progresses. However, there are specific rules that must be followed when using a for-of loop, one of which is related to variable declarations.

In a for-of loop, the loop variable is declared in the head of the loop, and its value is assigned as each iterable object's entry is encountered. Here's an example:

const numbers = [1, 2, 3, 4, 5];
for (let number of numbers) {
console.log(number);
}

In this example, the variable number is declared and assigned the value of each element in the numbers array as the loop iterates. However, Note that that a declaration in the head of a for-of loop cannot have an initializer. This means you cannot assign a default value to the loop variable before the loop starts.

Common Mistakes

  1. Trying to initialize the loop variable with a default value:
const numbers = [1, 2, 3, 4, 5];
for (let number = 0; number < numbers.length; number++) {
console.log(numbers[number]);
}

In this example, the loop variable number is being initialized with a default value of 0, which will cause the "SyntaxError: a declaration in the head of a for-of loop can't have an initializer" error. To fix this issue, remove the initialization part from the for-of loop header and use the let keyword to declare the variable within the loop body instead.

  1. Using the wrong loop construct:

If you need to initialize a variable with a default value before iterating over an array or other iterable object, consider using a traditional for loop or a for-of loop with an initializer outside the loop header.

const numbers = [1, 2, 3, 4, 5];
let i = 0;
for (i < numbers.length; i++) {
const number = numbers[i];
console.log(number);
}

In this example, the variable i is initialized outside the loop header with a default value of 0, and then used within the loop to access each element in the numbers array.

  1. Forgetting to declare the loop variable:

If you forget to declare the loop variable before using it in the for-of loop, JavaScript will automatically create a new variable with the same name in the current scope. However, this can lead to unexpected behavior and is best avoided by explicitly declaring the loop variable using the let keyword.

const numbers = [1, 2, 3, 4, 5];
for (let number of numbers) {
console.log(number);
}

In this example, the loop variable number is declared using the let keyword before being used in the for-of loop.

Worked Example

Let's take a look at an example that demonstrates the correct use of a for-of loop and how to avoid the "SyntaxError: a declaration in the head of a for-of loop can't have an initializer" error.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>For-Of Loop Example</title>
</head>
<body>
<script>
const numbers = [1, 2, 3, 4, 5];
let sum = 0;
for (let number of numbers) {
sum += number;
}
console.log(sum);
</script>
</body>
</html>

In this example, the numbers array is declared and iterated over using a for-of loop. The loop variable number is correctly declared within the loop header and assigned the value of each element in the array as the loop progresses. The sum of all numbers is calculated by adding each number to the sum variable, which is initialized outside the loop with a default value of 0.

Common Mistakes

  1. Trying to initialize the loop variable with a default value:
  • This will cause the "SyntaxError: a declaration in the head of a for-of loop can't have an initializer" error. To fix this issue, remove the initialization part from the for-of loop header and use the let keyword to declare the variable within the loop body instead.
  1. Using the wrong loop construct:
  • If you need to initialize a variable with a default value before iterating over an array or other iterable object, consider using a traditional for loop or a for-of loop with an initializer outside the loop header.
  1. Forgetting to declare the loop variable:
  • If you forget to declare the loop variable before using it in the for-of loop, JavaScript will automatically create a new variable with the same name in the current scope. However, this can lead to unexpected behavior and is best avoided by explicitly declaring the loop variable using the let keyword.
  1. Assigning a value to the loop variable outside the loop:
  • If you assign a value to the loop variable before the for-of loop starts, it will overwrite the initial value of the loop variable and cause unexpected behavior during the iteration process.
  1. Using a non-enumerable property as the loop variable:
  • For-of loops only iterate over enumerable properties of an object. If you try to use a non-enumerable property as the loop variable, it will not be included in the iteration process. To include non-enumerable properties, use a for...in loop instead.

Practice Questions

  1. Given the following code snippet, what error will be thrown, and how can it be fixed?
const numbers = [1, 2, 3, 4, 5];
for (let number = 0; number < numbers.length; number++) {
console.log(numbers[number]);
}

Answer: The code will throw a "SyntaxError: a declaration in the head of a for-of loop can't have an initializer" error. To fix this issue, remove the initialization part from the for-of loop header and use the let keyword to declare the variable within the loop body instead.

  1. Write a JavaScript function that takes an array of numbers as an argument and returns the sum of all even numbers using a for-of loop.

Answer:

function sumEvenNumbers(numbers) {
let sum = 0;
for (let number of numbers) {
if (number % 2 === 0) {
sum += number;
}
}
return sum;
}

FAQ

Can I initialize a loop variable with a default value using a for-of loop?

Answer: No, you cannot initialize a loop variable with a default value in the head of a for-of loop. Instead, use a traditional for loop or declare and initialize the variable outside the loop header before using it within the loop body.

What happens if I accidentally initialize a loop variable with a value that is not present in the iterable object?

Answer: If you initialize a loop variable with a value that is not present in the iterable object, the loop will still run the specified number of times based on the length of the iterable object. However, any iteration where the loop variable's value does not correspond to an actual element in the iterable object will result in undefined behavior.

Can I use a for-of loop with objects instead of arrays?

Answer: Yes, you can use a for-of loop with objects as well as arrays. The loop will iterate over each property of the object and assign its value to the loop variable. Keep in mind that the properties must be enumerable and not symbols.

How can I handle errors when using a for-of loop?

Answer: You can use try-catch blocks to handle errors within a for-of loop, just like you would with any other JavaScript code. This allows you to catch and handle specific errors as they occur during the iteration process.

Is it possible to skip certain iterations in a for-of loop?

Answer: Yes, you can use the continue statement within a for-of loop to skip over a specific iteration based on certain conditions. This is useful when you want to skip over specific elements in an array or object during the iteration process.

SyntaxError: a declaration in the head of a for-of loop can't have an initializer | Web Development | XQA Learn