In this chapter, we will learn about the JavaScript for
loop. The for
loop is used to execute a block of code a specified number of times. We will cover:
- What is a
for
Loop? - Syntax
- Using the
for
Loop - Iterating Over Arrays
- Iterating Over Objects
- Nested
for
Loops - Breaking and Continuing in Loops
- Simple Programs using
for
Loops
What is a for Loop?
The for
loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. It is particularly useful for iterating over arrays and other collections.
Syntax
for (initialization; condition; increment) { // code to be executed }
initialization
: Initializes one or more loop counters and is executed only once.condition
: Evaluated before each iteration. Iftrue
, the loop continues. Iffalse
, the loop ends.increment
: Updates the loop counter after each iteration.
Using the for Loop
Example
for (let i = 0; i < 5; i++) { console.log(i); }
Output:
0 1 2 3 4
In the example above, the loop starts with i = 0
, increments i
by 1 after each iteration, and stops when i
reaches 5.
Iterating Over Arrays
The for
loop is often used to iterate over arrays.
Example
let fruits = ["apple", "banana", "mango", "orange"]; for (let i = 0; i < fruits.length; i++) { console.log(fruits[i]); }
Output:
apple banana mango orange
In the example above, the loop iterates over the fruits
array and prints each element.
Iterating Over Objects
To iterate over the properties of an object, you can use a for...in
loop.
Example
let person = { firstName: "Ramesh", lastName: "Fadatare", age: 30 }; for (let key in person) { console.log(key + ": " + person[key]); }
Output:
firstName: Ramesh lastName: Fadatare age: 30
In the example above, the for...in
loop iterates over the properties of the person
object and prints each key-value pair.
Nested for Loops
You can use nested for
loops to handle more complex scenarios, such as iterating over multi-dimensional arrays.
Example
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]); } }
Output:
1 2 3 4 5 6 7 8 9
In the example above, the outer loop iterates over the rows of the matrix, while the inner loop iterates over the columns.
Breaking and Continuing in Loops
The break
statement is used to exit a loop early, while the continue
statement is used to skip the current iteration and continue with the next iteration.
Example with break
for (let i = 0; i < 10; i++) { if (i === 5) { break; } console.log(i); }
Output:
0 1 2 3 4
Example with continue
for (let i = 0; i < 10; i++) { if (i === 5) { continue; } console.log(i); }
Output:
0 1 2 3 4 6 7 8 9
Simple Programs using for Loops
Program 1: Print Numbers from 1 to 10
for (let i = 1; i <= 10; i++) { console.log(i); }
Output:
1 2 3 4 5 6 7 8 9 10
Program 2: Calculate the Sum of the First 10 Natural Numbers
let sum = 0; for (let i = 1; i <= 10; i++) { sum += i; } console.log("Sum:", sum);
Output:
Sum: 55
Program 3: Print the Elements of an Array in Reverse Order
let numbers = [1, 2, 3, 4, 5]; for (let i = numbers.length - 1; i >= 0; i--) { console.log(numbers[i]); }
Output:
5 4 3 2 1
Program 4: Multiplication Table for a Given Number
let number = 5; for (let i = 1; i <= 10; i++) { console.log(number + " * " + i + " = " + number * i); }
Output:
5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45 5 * 10 = 50
Program 5: Find the Factorial of a Number
let number = 5; let factorial = 1; for (let i = 1; i <= number; i++) { factorial *= i; } console.log("Factorial:", factorial);
Output:
Factorial: 120
Conclusion
In this chapter, you learned about the JavaScript for
loop, including its syntax, how to use it, iterating over arrays and objects, nested for
loops, and breaking and continuing in loops. We also covered various use cases with simple programs to demonstrate the usage of for
loops. The for
loop is used for repeating tasks and iterating over collections.