JavaScript basics loops

I'm a full-stack developer from South Africa ๐ฟ๐ฆ. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa ๐ฟ๐ฆ. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

In today's article on JavaScript basics, we'll be looking at different ways to create loops in JavaScript.
A loop is a way to iterate over code or execute code x times.
The different types of loops in JavaScript are:
forforEachfor...infor...ofwhiledo...whileI would say this is the godfather of loops. The basic for loop. Often you'll see this being used to loop over an array or execute code x times.
Let's first look at how we can create a loop that will execute five times.
for (let i = 0; i < 5; i++) {
// Execute 5 times
console.log(`This is loop number ${i}`);
}
// This is loop number 0
// This is loop number 1
// This is loop number 2
// This is loop number 3
// This is loop number 4
However, often we want to loop over an array of items. Let's say we have some foods and want to loop each view.
const foods = ['๐', '๐', '๐', '๐ฎ'];
for (let i = 0; i < foods.length; i++) {
console.log(foods[i]);
}
// ๐
// ๐
// ๐
// ๐ฎ
Ever since ES6 came out, we were introduced to the forEach method, making looping arrays way easier!
foods.forEach((item, index) => {
console.log(`${index}: ${item}`);
});
// 0: ๐
// 1: ๐
// 2: ๐
// 3: ๐ฎ
Or as a one-liner:
foods.forEach((item) => console.log(item));
Another cool thing we can do is loop through the properties of an object!
Let's say we want to loop each property of this user object.
const user = {
username: 'DailyDevTips',
firstName: 'Chris',
favoriteFood: '๐',
};
for (let property in user) {
console.log(`${property}: ${user[property]}`);
}
// username: DailyDevTips
// firstName: Chris
// favoriteFood: ๐
Then we also have the for...of loop, which can iterate over specific values instead of the properties.
const foods = ['๐', '๐', '๐', '๐ฎ'];
for (let value of foods) {
console.log(value);
}
The next big thing in loops is the while loop. This means code is executed while a condition is not met.
For instance, let's say we have a boolean value, and we should execute code until it's true.
let check = false;
while (!check) {
console.log('not correct');
check = true;
}
In this case, the code will execute once, be aware that this is a super-easy way to make an infinite loop that will crash your code!
With this, we can also evaluate a count, for instance, and only stop once the count is 5.
let amount = 0;
while (amount < 5) {
console.log(`amount ${amount}`);
amount++;
}
// amount 0
// amount 1
// amount 2
// amount 3
// amount 4
The do...while is very similar to the while loop, but the executing order differs.
Let's first look at how it works:
let test = true;
do {
console.log('testing');
test = false;
} while (test);
// testing
This will now execute once and evaluate that the test is not false. However, what happens when we start with the test being false?
let test = false;
do {
console.log('testing');
test = false;
} while (test);
// testing
Huh? This still logs testing. And yes it does
The do...while loop executes the code and THEN evaluates the while statement.
The while loop evaluates this code first before executing anything.
I hope you learned a thing or two about JavaScript loops!
I placed this code on a CodePen for you to check out and have a play around with.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter