Loops in JavaScript !!📚🔄

Loops in JavaScript !!📚🔄

What is Loop in JavaScript ?

Ans: Basically loop use for do repeatedly run a block of code – until a certain condition is met.

Types of Loops

For Loop

When to Use: When you know how many times you want to repeat something.

How It Works: You start with a counter, repeat code as long as the counter is less than a limit, and update the counter each time.

Example:

while Loop

When To use: When you don’t know exactly how many times you need to repeat something, but you have a condition to check

*How It Works: *

The condition is checked.

If the condition is true, the code inside the loop is executed.

The condition is checked again.

Steps 2 and 3 repeat until the condition becomes false.

Example:

Do-While Loop

When To use: Similar to a while loop, but the code inside the loop always runs at least once.

*How It Works: *

The code inside the do block is executed once before any condition is checked.

After executing the code block, the while condition is evaluated.

If the condition evaluates to true, the loop executes the code block again. If the condition evaluates to false, the loop terminates, and the program continues with the code that follows the loop.

Example:

For In Loop

When To use: The for…in loop helps you go through each item in an object or an array. It’s like checking each item one by one.

*How It Works: *

The loop starts by creating a variable (often called key) to store the current property name.

The loop checks if the current property exists in the object.

If the property exists, the code inside the loop is executed.

The loop moves to the next property.

Example:

For of Loop

When To use: When you want to loop through values of an array or other iterable.

*How It Works: *

The loop creates a variable to hold the current value.

The loop iterates over each value in the iterable object.

The current value is assigned to the variable.

Example:

Note: If you Know more please share.

Mail: mdabdurrazzakrakib290@gamil.com

Please follow and like us:
Pin Share