What is the IF statement And Switch Statement in JavaScript?

If Statement

It is used to execute an instruction or block of instructions only if a condition is fulfilled.

Syntax: –

if (condition)
      {
           block of statements;
      }

If statement with logical operator

if ( (condition1) && (condition2) )
     {
        block of statements;
     }

If else Statement

if… else statement is used when a different sequence of instructions is to be executed depending
on the logical value(True/False) of the condition evaluated.

Syntax: –

if(condition)
   {
     Statement_1_Block;
   }
else
   {
     Statement_2_Block;
   }
   Statement_3;

Else If Statement

To show a multi-way decision based on several conditions, we use else if statement.

Syntax: –

If(condition_1)
   {
     statements_1_Block;
   }
else if(condition_2)
   {
     statement_2_Blocks;
   }
else if(condition_n)
   {
     Statements_n_Block;
   }
else
 statements_x;

Switch Statement

Check several possible constant values for an expression.

Syntax: –

switch(expression){
      case expression 1:
            block of statements 1;
            break;
      case expression2;:
            block of statements 2;
            break;
            .
            .
      default:
            default block of instructions;
      }

All the cases will be evaluated if you don’t use a break statement.

switch(expression){
            case expression 1:
                 block of statements 1;
                 break;
            case expression 2:
            case expression 3:
                 block of statements 2;
                 break;
            default:
                 default block of instructions;
            }

Tagged : / / /

Leave a Reply