If – Else or Conditional Statements in JavaScript:

In any programming language Conditional statements are used to perform different type of condition based operations.

JavaScript have following conditional statements:

  • If statement
  • If – else statement
  • Else if statement
  • Switch statement

Note:

In this topic we will learn if , if-else and else if statements and we will cover switch statement later in this tutorial.

The if Statement:

The If statement in JavaScript is use to execute the block of code when condition is true.

Syntax:

 Example:

 

The if-else Statement:

The If-else statement in JavaScript is use to execute the block of code when the condition is true than the code written in if statement block will execute otherwise the code which written in else block will be executed.

Syntax:

 Example:

The if-else if Statement:

In else if statement you can assign another condition and it will execute if the first condition is false which is written in if block and if both if and else if conditions are false than the else block will be executed.

Syntax:

 Example:

Leave a Reply

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