JavaScript ( Loops, Conditions )

Conditions

Conditional statements are used in JavaScript to execute different code blocks based on whether a condition is true or false. The most commonly used conditional statements in JavaScript are:

  • if else

  • Switch case

  • Ternary operator

If Else

  1. if statement: This statement is used to execute a block of code if a condition is true.

     let age = 18;
     if (age >= 18){
         console.log("You are eligible for vote.")
     }
    
  2. if else statement: This statement is used to execute one block of code if a condition is true, and another block of code if the condition is false.

     let age = 18;
     if (age >= 18){
         console.log("You are eligible for vote.")
     } else {
         console.log("You are underage.")
     }
    
  3. if...else if statement: This statement is used to test multiple conditions and execute different code blocks depending on which condition is true.

     let grade = 75;
     if (grade >= 90) {
       console.log("You got an A.");
     } else if (grade >= 80) {
       console.log("You got a B.");
     } else if (grade >= 70) {
       console.log("You got a C.");
     } else {
       console.log("You got a D or lower.");
     }
    

Switch Case

The switch statement is a conditional statement in JavaScript that is used to execute different code blocks based on the value of an expression. It is often used as an alternative to multiple if...else if statements when you have a large number of possible values.

let day = "Monday";
switch (day) {
  case "Monday":
    console.log("Today is Monday.");
    break;
  case "Tuesday":
    console.log("Today is Tuesday.");
    break;
  case "Wednesday":
    console.log("Today is Wednesday.");
    break;
  default:
    console.log("Today is not Monday, Tuesday, or Wednesday.");
}

Ternary operator / Ternary conditions

The ternary operator is a shorthand way of writing an if...else statement in JavaScript. It allows you to write a single line of code that performs a conditional operation based on a true or false evaluation. The syntax of the ternary operator is as follows:

//Condition ? Expression1 : Expression2

let age = 18;
let isEligibleToVote = (age >= 18) ? "Yes" : "No";
console.log("Is the person eligible to vote? " + isEligibleToVote);

Loops

Do while

The do...while loop is similar to the while loop, but the condition is checked after the code inside the loop is executed, which means the code inside the loop is executed at least once. Here's the basic syntax of a do...while loop:

do {
  // code to be executed
} while (condition);

The code inside the loop is executed first, and then the condition is checked. If it is true, the loop continues, otherwise the loop stops. Here's an example of a do...while loop that prints the numbers from 1 to 5:

let i = 1;
do {
  console.log(i);
  i++;  
} while (i <= 5);

While loop

The while loop is used to loop through a block of code as long as a certain condition is true. Here's the basic syntax of a while loop:

while (condition) {
  // code to be executed
}

The condition is checked before each iteration of the loop, and if it is true, the code inside the loop is executed. Here's an example of a while loop that prints the numbers from 1 to 5:

let i = 1;
while (i <= 5) {
  console.log(i);
  i++;
}

For loop

The for loop is the most common type of loop in JavaScript. It allows you to loop through a block of code a specified number of times. Here's the basic syntax of a for loop:

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

The initialization statement is executed once before the loop starts. The condition is checked before each iteration of the loop, and if it is true, the code inside the loop is executed. The increment/decrement statement is executed after each iteration of the loop. Here's an example of a for loop that prints the numbers from 1 to 5:

for (let i = 1; i <= 5; i++) {
  console.log(i);
}