JavaScript Loops
Introduction (Reference Link: [Link]
javascript-loops/ )
In the field of programming language, the loop is a basic fundamental concept
that is used to repeat a particular set of code or instructions till the certain
condition given is met. In simpler words, with the help of loops we are able to
execute repetitive tasks with the help of a single code block instead of writing
the code again and again. These loops terminates when the condition meets
false.
Definition (Reference Link: [Link]
javascript-loops/ )
JavaScript loops are the programming structure with the help of which we can
implement some repetitive tasks by writing a block of code. The loops are used
to code that part of programs or applications that needs to be repeated avoid to
writing the code again and again. Loops usually repeat between the data
structure given like arrays or objects. In simple words, a loop repeats itself until
the certain given criteria of the condition are met or it becomes true as a result.
Why to use Loops? (Reference Link:
[Link] )
Loops are used as with the help of loops we can make our code shorter and
more efficient by handling multiple tasks simultaneously. And due to the
working of it, that is, automatically. The main reasons to use loops are:
1. Avoid Repeating Code – Instead of writing the same code many times, we
can write a single or few line code and make it run multiple times till required
by using loops which makes our work easier.
2. Work with Large Amounts of Data – Suppose we have a large amount of
data like lists, dictionaries or objects, we cannot write different codes for
different lines. Then the loops help and make it easy to go through the entire list
or items automatically and find out what we exactly want.
3. Perform Repeated Calculations or Tasks – The loops serve a great purpose
for tasks like searching, counting or updating values as they give exact values
and the results without giving much effort and writing code in repetitive
manner.
4. Make Code More Dynamic – Loops allow the program to respond to any
changes that have been made to the data or conditions.
5. Save Time and Reduce Errors – Using loops avoids errors because the code
is not being repeated, it is only applied once, and also it saves time as it is much
faster than writing the entire code again and again. It works much faster than us.
Types of Loops in JavaScript
There are mainly five types of loops in JavaScript. Here are the loop types:
1. for Loop – It consists of a fixed number of repetitions.
2. while Loop – It repeats while the given condition is true.
3. do-while Loop – This loop must have at least one repetition.
4. for-of Loop – This involves looping through array or string values.
5. for-in Loop – This involves looping through object properties.
About the Loops (Reference Link:
[Link] )
1. for loop
For loop is used to perform a definite repetitive task until a given condition.
This loop is best when you know the exact number of times the loop should be
executed, as this loop demands the exact number of repetitions that are to be
done.
Syntax
for (initialization; condition; update) {
// code to be executed
}
Where,
• The initialization part runs once before the loop is about to start. It refers to the
value from where the loop should start.
• condition is the part that is checked before the loop iteration. The loop
executes until the condition is true then it breaks.
• update is the change applied to the initial value after the iteration of the loop
every time. (e.g., i++, the value of i increases by 1 every time.)
Example: Print numbers from 1 to 5
for (let i=1; i<=5; i++) {
[Link](i);
}
Output:
1
2
3
4
5
2. while Loop
Same as for loop, while a loop in JavaScript is also used to repeat the block of
code till the given condition is true. But unlike for loop, while loop is better
used when the number of repetitions of the loop is not defined or we can say the
number of repetitions is unknown.
Syntax
while (condition) {
// code to be executed
}
Where,
• condition is evaluated before each iteration. If the condition is true, the code
inside the loop will run simultaneously, and the loop stops when the given
condition becomes false.
Example: Print numbers from 1 to 5
let i=1;
while (i<=5) {
[Link] (i);
i++;
}
Output:
1
2
3
4
5
3. do…while Loop
A do…while loop in JavaScript is used to execute that particular block of code
at least once, and after that repeat it till the specified condition is true. It is
similar to a while loop but there is one difference, that is, the code inside this
loop will run at least once even if the condition in it is false on the first iteration.
Syntax
do {
// code to be executed
} while (condition);
Where,
The code inside do{} will run once and after that the condition will be checked,
if it is true then the loop will continue, otherwise, it will stop.
Example: Print numbers from 1 to 5.
let i=1;
do {
[Link](i);
i++;
}while (i<=5);
Output:
1
2
3
4
5
4. for…of Loop
The for…of loop in JavaScript is mainly used to iterate the values of arrays,
strings, etc. for…of loop gives us direct access to use each value in the
collection which makes it more efficient and easier to read.
Syntax
for (let variable of iterable) {
// code to be executed for each value
}
where,
The variable is the name that stores each item from the iterable and iterable is
the main data structure from which the loop will run.
Example: Loop through an Array
let fruits = [“apple”, “banana”, “cherry”];
for (let fruit of fruits) {
[Link](fruit);
}
Output:
apple
banana
cherry
5. for…in Loop
The for…in loop in JavaScript is used to iterate countable properties of an
object.
Syntax
for (let key in object) {
//code to be executed for each key
}
where,
The key is the variable that shows each property name and the object is
something whose properties you want to loop through.
Example: Loop Through an Object
let person = {
name: “Alice”,
age: 25;
city: “New York”
};
for (let key in person) {
[Link](key + “:” + person[key]);
}
Output:
name: Alice
age: 25
city: New York
Conclusion
JavaScript loops are a very useful tool for writing efficient and reusable code. A
code written once can perform a task multiple times with the help of a loop. It
basically helps to keep the code easier to manage without to need for repetition
on our very own. Loops are a core part of development as they allow us to
simplify things and make the program mode dynamic and interactive.