
7 Loop Types in JavaScript.
In this Post, we'll go through different ways of Looping In Javascript with the help of examples.
- for
- while
- do-while
- forEach
- map
- for-of
- for-in
JavaScript for loop:
Example-
for (let i = 0; i < 5; i++) {
text += "The number is " + i + "<br>";
}
JavaScript while loop:
Example-
while (i < 10) {
text += "The number is " + i;
i++;
}
JavaScript do-while loop:
Example-
do {
text += "The number is " + i;
i++;
}
while (i < 10);
JavaScript forEach loop:
Example-
let sum = 0;
const numbers = [65, 44, 12, 4];
numbers.forEach(myFunction);
function myFunction(item) {
sum += item;
}
JavaScript map:
Example-
const cars = ['TaTa', 'Volvo', 'BMW'];
const newCars = cars.map((ele) => {
return ele.toUpperCase()
});
JavaScript for-of loop:
Example-
const cars = ['TaTa', 'Volvo', 'BMW'];
for (let ele of cars) {
console.log(ele);
}
JavaScript for-in loop:
Example-
const person = {fname:"John", lname:"Doe", age:25};
let text = "";
for (let x in person) {
text += person[x];
}
Like, Share and Subscribe #DevTools99 for more useful videos, tools info and #tutorials . Thank you!