Conditionals in JavaScript

JavaScript Conditionals are statements that control behavior in JavaScript and determine whether code snippets can be executed or not.

There are several types of conditionals in JavaScript, including:

  • “If” statements: If a condition is true, it is used to Specify the execution of a block of code.
  • Else statements: If the same condition is false, specify the execution of a block of code.
  • Else if statements: This specifies a new test if the first condition is false.
  • Switch statement is used to perform totally different actions supported different conditions.

Now that you just have the fundamental JavaScript conditional statement definitions, let’s show you samples of each.

1.  The “if” Statement:

Syntax of  “if “:

// Syntax of if statement
if(condition){
  // This block executes when condition inside if is true
}

Example:

// Example of if statement
let age = 24
if(age >= 18){
   console.log("You are eligible for driving");  
}
// Output: You are eligible for driving

2.  The “else” Statement:

Use the else to specify a block of code to run if the condition is false.

Syntax:

// Syntax of else
if (condition) {
  //  This block executes when condition inside if is true
} else {
  //  This block executes when condition inside if is false
}

Example:

// Example of if statement 
let age = 14 
if(age >= 18){ 
   console.log("You are eligible for driving"); 
} else {
   console.log("Not eligible for driving"); 
}
// Output: You are not eligible for driving

3. The “else if” Statement

Use the else if statement to specify a brand new condition if the primary condition is false.

Syntax:

// syntax of else-if
if (condition1) {
  //  This block executes when condition1 inside if is true
} else if (condition2) {
  //  This block executes when condition2 inside else if is true
} else {
  //  This block executes when condition1 and condition2 both are false
}

Example:

// check if the number if positive, negative or zero
let num = -5
// check if number is greater than 0
if (num > 0) {
    console.log("The number is positive");
}
// check if number is 0
else if (num == 0) {
  console.log("The number is 0");
}
// if number is neither greater than 0, nor zero
else {
    console.log("The number is negative");
}

// Output: The number is negative

4. The “switch” Statement

Use the switch to select one of many blocks of code to run.

Syntax:

//Example of switch statement
switch(expression) {
  case x:
    // statements
    break;
  case y:
    // statements
    break;
  default:
    // statements
}

Example:

//Example of switch statement
let num = 4;

switch (num) {
  case 3:
    alert( 'Too small' );
    break;
  case 4:
    alert( 'Exactly!' );
    break;
  case 5:
    alert( 'Too big' );
    break;
  default:
    alert( "I don't know such values" );
}

Thank you so much for reading

Happy Coding

For detailed explanation visit MDN Docs

Some other Articles on JavaScript

Leave a Reply

Your email address will not be published. Required fields are marked *