DEV Community

Cover image for For and while loops in Javascript.
Ali Sina Yousofi
Ali Sina Yousofi

Posted on

1

For and while loops in Javascript.

Introduction

Loops are an essential feature of any programming language, and JavaScript is no exception. They allow developers to execute a block of code repeatedly until a certain condition is met. In this blog, we will explore the different types of loops in JavaScript and how to use them effectively.

1. For loop

The for loop is one of the most commonly used loops in JavaScript. It allows you to execute a block of code repeatedly until a specific condition is met. The syntax of the for loop is as follows:

Image description

The initialization step is used to declare and initialize a variable that controls the loop. The condition is checked before each iteration, and if it is true, the loop continues. The increment step is executed after each iteration and is used to update the control variable.

2 / 2

Loops are an essential feature of any programming language, and JavaScript is no exception. They allow developers to execute a block of code repeatedly until a certain condition is met. In this blog, we will explore the different types of loops in JavaScript and how to use them effectively.

for Loop
Enter fullscreen mode Exit fullscreen mode

The for loop is one of the most commonly used loops in JavaScript. It allows you to execute a block of code repeatedly until a specific condition is met. The syntax of the for loop is as follows:

css

for (initialization; condition; increment) {
// code to be executed
}

The initialization step is used to declare and initialize a variable that controls the loop. The condition is checked before each iteration, and if it is true, the loop continues. The increment step is executed after each iteration and is used to update the control variable.

Here's an example that uses a for loop to iterate through an array of numbers and print them to the console:

Image description

Factorail program using for loop

Image description

In this example, the factorial() function takes an integer n as an input and returns the factorial of n. The function initializes the result variable to 1 and then uses a for loop to multiply the result variable by each integer from 1 to n.

Inside the for loop, we use the compound assignment operator *= to multiply the result variable by the current integer (i.e., result *= i is equivalent to result = result * i).

Finally, we return the final value of the result variable, which represents the factorial of n.

While loop

A while loop is a control flow statement in JavaScript that allows you to execute a block of code repeatedly as long as a specified condition is true. Here's the basic syntax of a while loop:

Image description;

The condition is a boolean expression that is evaluated before each iteration of the loop. If the condition is true, the code inside the loop is executed. If the condition is false, the loop is terminated and the program continues with the next statement after the loop.

Here's an example of using a while loop to print out the first five positive integers:

Image description

In this example, we initialize the variable i to 1 outside the while loop. Inside the while loop, we check if i is less than or equal to 5. If the condition is true, we print the value of i using console.log() and then increment i by 1 using the ++ operator.

The loop continues to execute as long as i is less than or equal to 5. Once i becomes 6, the condition becomes false and the loop is terminated.

While loops are useful for executing a block of code repeatedly as long as a certain condition is true. They are commonly used for tasks such as iterating over arrays, reading data from a file, and waiting for user input. However, be careful not to create an infinite loop where the condition is always true, as this can cause your program to crash.

Image of Quadratic

AI, code, and data connections in a familiar spreadsheet UI

Simplify data analysis by connecting directly to your database or API, writing code, and using the latest LLMs.

Try Quadratic free

Top comments (0)

Jetbrains image

Is Your CI/CD Server a Prime Target for Attack?

57% of organizations have suffered from a security incident related to DevOps toolchain exposures. It makes sense—CI/CD servers have access to source code, a highly valuable asset. Is yours secure? Check out nine practical tips to protect your CI/CD.

Learn more

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay