Javascript Loops

Loops are one of the most valuable tools in any programmer's arsenal, and Javascript is no exception. Loops allow you to execute the same code multiple times with different values, making them a great way to deal with repetitive tasks. There are three main types of loops in Javascript: for loops, while loops, and do-while loops, each with its own syntax and usage.

For Loops

For loops are the most popular looping option in Javascript. They are used to iterate through a set of values or objects and have three components: an initializer, a condition check, and an incrementer. The initializer is used to set up a variable to be used in the loop, the condition check determines when the loop should end, and the incrementer is used to update the variable for the next iteration.

for (let i = 0; i < num; i++) {
    // do something

While Loops

While loops are similar to for loops, but only include a condition check. This means that the loop will keep running until the condition is no longer valid. While these loops can be useful, they can also be dangerous if the condition is not set up correctly, as they can cause an infinite loop.

let i = 0;
while (i < 100) {

Do-While Loops

Finally, do-while loops are a combination of for and while loops. These loops always run at least once, regardless of what the condition check returns. This can be useful when you need to make sure some code is run at least once, such as when prompting the user for input.

let result = "";
let i = 0;
do {
  i += 1;
  result += `${i} `;
} while (i > 0 && i < 5);
// Despite i === 0 this will still loop as it starts off without the test


No matter which loop type you use, understanding the basics of looping in Javascript is the first step to mastering them. With some practice, you can learn more advanced techniques, such as using for...in and for...of loops to iterate over objects and arrays, or using break and continue statements to control the flow of the loop. With these techniques, your loops will become more powerful and efficient.



Be the first to comment!

Read More

Seeded Random Number Generator in JS

Random numbers are an essential tool for developers, whether it's for generating random colors for page elements, creating dynamic content on canvas, or any other purpose. However, one issue with the default Math.random() function is you can not repeat the results. T...

JS Chunk Array

Here's a simple function that uses generators to chunk an array. Generators are a type of function that returns an iterator and traverse it one at a time if you u...

Dynamically loading Google Fonts

Google Fonts is a great tool for web developers, it allows you to load in almost any font on to you website and use it with only a few lines of cod...

Express Plugins

Express plugins can be used to process POST body data, serve static files, or serve content with CORS enabled. Learn the basics of app.use.