How to use different types of loops in JavaScript?

Today I will explain how to use different types of loops in JavaScript, A different ways to producing

Same looping behavior.

Now we will look over the syntax for loop, here for loop looks similar to all other
Coding syntaxes.

<script>

For (Var number = 0;   number <= 10; number++)
{
Console.log (“Display the numbers here +number+”);
}

</script>

In the above loop first statement will initializing the variable, the second statement will check the condition just like while loop, and third statement will control the loop ex: incrementing or decrementing the variable values.

Here in the above example the loop counter will initialized as part of the loop itself.
The loop check for an expression to see if another loop is execute, after body code is executed the final expression is executed before moving on, this statement will affect weather to run loop again.
Before another loop the middle expression will check again.

Now we will check another if and else code based on specific conditions.

Syntax for if and else condition.

<script>

Var  valueone=5;
Var valuetwo=6;

If( valueone > valuetwo)
{
    Console.log(“condition will print +valueone+ is  greater”);
}else
{
    Console.log(“condition will print +valuetwo+ is greater”);
}
</script>

If the condition evaluates to true, the if code block will execute code inside the if block.
Else follows up with code to execute only when the if condition is not satisfied, it is ignored otherwise.
Now we will look over the nested if and else conditions below, syntax for nested if else conditions.

<script>
If(some condition here)
{
}else if(some other condition here)
{
}else
{
}
</script>

As soon as condition is met in any block, the rest of the condition will be skipped completely and print the output of particular executed condition.
Elseif is mostly used to check multiple conditions easily
Now we will check the multiple conditions using else if as below

<script>
Var Teachers=11;
For(Var  classes=10; classes<= Teachers;  classes++)
{
If ( classes <=Teachers)
{
 Console.log(“show all classes starts from +classes+”);
}
Else if(classes == 10)
{
 Console.log(“Higher section class is 10”);

}else
{
Console.log(“10 is not higher class”);
}

}
</script>

Thanks for reading this article

READ  Why React Native Could Gain Rapid Popularity Along With Credibility

One Response

Leave a Reply