Last Updated on March 10, 2024 by
JavaScript Loops
Hello Bloggers, Welcome to Decode Now. In this blog we will discuss about JavaScript Loops. We’ll embark on a journey into the world of JavaScript loops, exploring their types, syntax, and practical applications.
– In JavaScript, loops are used to perform repeated tasks based on a condition.
A loop is a programming tool that is used to repeat a set of instructions.
– There are four types of loops in JavaScript are:-
1. for loop
2. for-in loop
3. for-of loop
4. while loop
5. do-while loop
– JavaScript Loop:- For Loop
-> A for loop is a control flow statement that allows you to repeatedly execute a block of code a specified number of times.
– The syntax of for loop is given below:
for (initialization; condition; increment)
{
code to be executed
}
– Here’s an example of a for loop that counts from 1 to 5 and prints the numbers:
-> Initialization: ‘let i = 1’ initializes a variable ‘i’ to 1.
-> Condition: ‘i <=5’ is the condition that checks if ‘i’ is less than or equal to 5.
-> Iteration: ‘i++’ increments the value of ‘i’ by 1 in each iteration.
The loop will execute as long as ‘i’ is less than or equal to 5, and it will print the numbers 1 through 5.
– JavaScript Loop:- For-in loop
-> The for-in loop iterates over the properties of an object.
– The syntax of for loop is given below:
for (variable in object) {
// Code to be executed for each property
}
– Here’s an example of using a ‘for-in’ loop to iterate over the properties of an object :
-> In this example, the ‘for-in loop iterates over the properties of the ‘person’ object, and for each property, it prints the property name and its corresponding value.
– JavaScript Loop:- For-of loop
-> The for-of loop is used for iterating over the values of iterable objects such as arrays, strings, maps, sets, and more.
-> A for-of loop operates on the values sourced from an iterable one by one in sequential order.
– The syntax of for-of loop is given below:
for (variable of iterable) {
// Code block to be executed
}
– Let’s see the example of for of loop in javascript:
const numbers = [1, 2, 3, 4, 5];
for (const number of numbers) {
console.log(number);
}
– JavaScript Loop:- While Loop
-> A while loop in JavaScript is a control flow statement that allows you to execute a block of code repeatedly as long as a specified condition evaluates to true.
– The syntax of while loop is given below:
while(condition) {
//Code block to be executed
}
– Let’s see the example of while loop in javascript:
<script>
var i=18;
while(i<=20)
{
document.write(i + “br/>”);
i++;
}
</script>
– JavaScript Loop:- Do-While Loop
-> The do-while loop executes the code block at least once, and then it checks the condition. This means that the code block is executed once before the condition is tested.
-> The syntax of do-while loop is given below:
do {
//Code block to be executed
} while (condition);
– Let’s see the example of do-while loop in javascript:
let count=0;
do {
console.log(count);
count++;
} while (count < 5)
Note :-
- If you want to learn Javascript Loops, Above are the key points which will help you to learn more about it.
- Also, if you like this post or have any queries, Do let me know in the comments.
- Below are the resources to learn more about Javascript.