1.

JavaScript If-Else Statements

Answer»

The if-else statements are simple to comprehend. You can use them to set conditions for when your code runs. If specific requirements are met, something is done; if they are not met, another action is taken. The switch statement is a concept that is comparable to if-else. The switch however allows you to choose which of several code blocks to run. The syntax of if-else statements in JavaScript is given below:

if (check condition) {
// block of code to be executed if the given condition is satisfied
} else {
// block of code to be executed if the given condition is not satisfied
}

Loops in JavaScript:

Most programming languages include loops. They let you run code blocks as many times as you like with different values. Loops can be created in a variety of ways in JavaScript:


  • the for loop: The most frequent method of creating a loop in JavaScript. Its syntax is shown below:
for (initialization of the loop variable; condition checking for the loop; updation after the loop) {
// code to be executed in loop
}

  • the while loop: Establishes the conditions under which a loop will run. Its syntax is shown below:
// Initialization of the loop variable is done before the while loop begins
while(condition checking for the loop){
// 1. code to be executed in loop
// 2. updation of the loop variable
}

  • the do-while loop: Similar to the while loop, but it runs at least once and checks at the end to see whether the condition is met to run again. Its syntax is shown below:
// Initialization of the loop variable is done before the do-while loop begins
do{
// 1. code to be executed in loop
// 2. updation of the loop variable
}while(condition checking for the loop);

There are two statements that are important in the context of loops:



  • the continue statement: Skip parts of the loop if certain conditions are met.


  • break statement: Used to stop and exit the cycle when specific conditions are met.




Discussion

No Comment Found