Conditional Statement in Javascript

Codynn
2 Min Read

Conditional Statements

Conditional statements are used to do various actions in response to various conditions ( weather condition is true or false.)

In JavaScript, there are three types of if statements.

  1. If Statement
  2. If else statement
  3. if else if statement
  1.   If Statement: An if statement directs the program to adopt one of two paths, based on the outcome of an expression evaluation.
if (true) {
    //action perform
  }
 
  if (false) {
    //action perform (? never ?)
  }
  1.  If else statement: It determines if the condition is true or false by evaluating the content.
if (true) {
    //action perform
  } else {
    //action perform else
  }
  1.  if else if statement: It specify a new condition to be tested, if the first condition is false.
if (condition1) {
    //  if condition1 is true
  } else if (condition2) {
    //  if the condition1 is false and condition2 is true
  } else {
    // if the condition1 is false and condition2 is false
  }

JavaScript Switch Statement

JavaScript Switch Statement: It’s performed to carry out a variety of tasks depending on the situation.

Syntax

 switch(expression) {
    case x:
      // code block
      break;
    case y:
      // code block
      break;
    default:
      // code block
  }
 

If a match is found, the associated code block is run otherwise

If no match is found, the default code block is performed.

Share This Article
Leave a comment

Leave a Reply

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