what is loop ?
A loop is like a set of instructions that we can repeat over and over again. It's like a cycle that allows us to do the same thing multiple times without writing the instructions again and again.
For example ,Imagine you have a task to write the numbers from 1 to 10. Instead of writing the number 10 times, you can use a loop. The loop will start at 1, write the number, and then go back and do the same thing for the next number, until it reaches 10. So, the loop save us from writing the same instructions multiple times.
Loops are really helpful when we want to do something repeatedly, like processing a list of items, performing calculations, or waiting for certain conditions to be met. They make programming more efficient and less repetitive by automating tasks that need to be done over and over again.
Different types of loops
In JavaScript, there are three main types of loops:
the for loop, the while loop, and the do...while loop, for in , for of . Here's the syntax for each:
For Loop:
The for loop is commonly used when we know the number of iterations .
Syntax:
for (initialization; condition; update) {
// code
}
initialization: Sets the initial value .
condition: Specifies the condition that must be true for the loop to continue.
update: Defines how the loop variable is updated after each iteration.
Example: Printing numbers from 1 to 5 using a for loop.
for (let i = 1; i <= 5; i++) {
console.log(i);
}
Output
1 2 3 4 5
While Loop:
The while loop is suitable when we have a condition that determines whether to continue or stop the loop. The loop will keep executing as long as the condition remains true.
Syntax:
initialization;
while (condition) {
// Code
update;
}
Example: Printing numbers from 1 to 5 using a while loop.
let i = 1;
while (i <= 5) {
console.log(i);
i++;
}
Output
1 2 3 4 5
Do...While Loop:
The do...while loop is similar to the while loop, but it always executes the code block at least once. The condition is checked after the first execution to determine if the loop should continue or stop.
Syntax:
do {
// Code
} while (condition);
Example: Printing numbers from 1 to 5 using a do...while loop.
let i = 1;
do {
console.log(i);
i++;
} while (i <= 5);
Output
1 2 3 4 5
For...of Loop:
The for...of loop is used to iterate over iterable objects such as arrays, strings, and other collections that are iterable. It allows you to directly access the individual elements of the collection without dealing with indices.
Syntax:
for (variable of iterable) {
// Code
}
variable: A variable that represents the current element value in each iteration.
iterable: The iterable object being iterated over.
Example:
const fruits = ['apple', 'banana', 'orange', 'mango'];
for (let fruit of fruits) {
console.log(fruit);
}
Output:
For...in Loop:
The for...in loop is used to iterate over the enumerable properties of an object. It iterates over the property names or keys of an object, including inherited properties from the object's prototype chain.
Syntax:
for (variable in object) {
// Code
}
variable: A variable that represents the current property name/key in each iteration.
object: The object whose properties are being iterated over.
Example:
const person = {
name: 'John',
age: 30,
profession: 'Developer'
};
for (let prop in person) {
console.log(prop + ': ' + person[prop]);
}
Output:
name: John
age: 30
These loops provide different ways to repeat code based on specific conditions and are essential for performing repetitive tasks and iterating over data structures in JavaScript.