Published On: January 8th, 2023Categories: AI News

Hello friends. In this script, I’m going to briefly talk about 6 loops in JS. So let’s get started!

Loops offer a quick and easy way to do something repeatedly. They can execute a block of code as long as a specified condition is true.



1. The While Loop

The while statement creates a loop that executes a specified statement as long as the test condition evaluates to true. The condition is evaluated before executing the statement.

//Example

const menu = ['Rice', 'Chicken', 'Burger', 'Pizza', 'Cheese cake'];

let i = 0;
while(i < menu.length) {
   console.log('I will eat' + menu[i])
   i++;
}

//Results
I will eat Rice
I will eat Chicken
I will eat Burger
I will eat Pizza
I will eat Cheese cake

Enter fullscreen mode

Exit fullscreen mode



2. The Do While Loop

The do…while statement creates a loop that executes a specified statement until the test condition evaluates to false. The condition is evaluated after…

Source link

[gs-fb-comments]

Leave A Comment