JavaScript uses the brackets ({}
) to indicate which lines of code are part of a for loop and which are not.
Take this code for example:
let languages = ["Python", "Java", "JavaScript"];
for (let language of languages) {
console.log("I love " + language);
console.log("I want to get better at it");
}
Both console.log
statements are part of the loop since they are written inside the brackets.
This means that both messages will be printed for every language
in the languages
list. The output of that code would be:
I love Python
I want to get better at it
I love Java
I want to get better at it
I love JavaScript
I want to get better at it
As for this code:
let languages = ["Python", "Java", "JavaScript"];
for (let language of languages) {
console.log("I love " + language);
}
console.log("I want to get better at them");
The console.log("I want to get better at them");
is not part of the loop.
This means that only console.log("I love " + language);
will be executed for every language
in the languages
list.
Then, when the for loop is over, the console.log("I want to get better at them");
will be executed only once.
The output of this program would be:
I love Python
I love Java
I love JavaScript
I want to get better at them
Execution flow
Lastly, note that the execution of a program always begins on the first line. The code is then executed one line at a time from top to bottom. This is known as execution flow and is the order a program in JavaScript executes code.
For example, this program:
let fruits = ["banana", "apple", "kiwi"];
console.log("I'm hungry!");
for (let fruit of fruits) {
console.log("I'm gonna eat:");
console.log(fruit);
}
console.log("I love my life!");
prints:
I'm hungry!
I'm gonna eat:
banana
I'm gonna eat:
apple
I'm gonna eat:
kiwi
I love my life!
Assignment:
Add a for loop inside our code in the editor such that the final program would print:
Today we'll learn about:
Python
Java
JavaScript
Let's code!
In this lesson, we will explore the for loop in JavaScript, a fundamental concept in programming that allows us to execute a block of code multiple times. Understanding how to use loops effectively is crucial for tasks such as iterating over arrays, automating repetitive tasks, and managing control flow in your programs.
For loops are particularly useful in scenarios where you need to process each item in a collection, such as an array, or when you need to execute a block of code a specific number of times.
A for loop in JavaScript consists of three main parts:
Here is a simple example:
for (let i = 0; i < 5; i++) {
console.log(i);
}
This loop will print the numbers 0 through 4. The loop starts with i
set to 0, runs as long as i
is less than 5, and increments i
by 1 after each iteration.
Let's break down the key concepts and techniques involved in using for loops:
let i = 0;
initializes i
to 0.i < 5
means the loop will run while i
is less than 5.i++
increments i
by 1 each time the loop runs.Here is an example that combines these concepts:
let languages = ["Python", "Java", "JavaScript"];
for (let i = 0; i < languages.length; i++) {
console.log("I love " + languages[i]);
}
This loop iterates over the languages
array and prints a message for each language.
Let's look at some examples and use cases for for loops:
Example 1: Iterating Over an Array
let fruits = ["banana", "apple", "kiwi"];
for (let i = 0; i < fruits.length; i++) {
console.log("I'm gonna eat: " + fruits[i]);
}
This loop prints a message for each fruit in the fruits
array.
Example 2: Summing Numbers
let sum = 0;
for (let i = 1; i <= 10; i++) {
sum += i;
}
console.log("The sum is: " + sum);
This loop calculates the sum of numbers from 1 to 10.
Here are some common mistakes to avoid and best practices to follow when using for loops:
Once you are comfortable with basic for loops, you can explore more advanced techniques:
break
and continue
to control the flow of your loops.Here is an example of a nested loop:
let matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[i].length; j++) {
console.log(matrix[i][j]);
}
}
Let's implement the assignment to print the desired output:
let languages = ["Python", "Java", "JavaScript"];
console.log("Today we'll learn about:");
for (let language of languages) {
console.log(language);
}
console.log("Let's code!");
This code will print:
Today we'll learn about:
Python
Java
JavaScript
Let's code!
When debugging loops, consider the following tips:
console.log
to print the loop variable and other relevant information.Here is an example of a test case for our loop:
function testLoop() {
let languages = ["Python", "Java", "JavaScript"];
let output = [];
output.push("Today we'll learn about:");
for (let language of languages) {
output.push(language);
}
output.push("Let's code!");
return output;
}
console.log(testLoop().join("\n"));
When approaching problems involving loops, consider the following strategies:
In this lesson, we covered the basics of for loops in JavaScript, including their structure, common use cases, and best practices. Mastering loops is essential for writing efficient and effective code. Keep practicing and exploring more advanced techniques to enhance your programming skills.
For further reading and practice, consider the following resources:
Our interactive tutorials and AI-assisted learning will help you master problem-solving skills and teach you the algorithms to know for coding interviews.
Start Coding for FREE