Skip to content

if else if JavaScript | Basic code

  • by

Use the if else if statement if you want to check 2 conditions in JavaScript. Use if to specify a block of code to be executed, if a specified condition is true.

And Use else if to specify a new condition to test, if the first condition is false

if (condition) {
	// statement
} else if(condition){
	// statement
  // statement

Example if else if JavaScript

A simple example code uses the else if statement to specify a new condition if the first condition is false.

<!DOCTYPE html>

    var greeting= "Hello"
    var time = 15;

    if (time < 10) {
      greeting = "Good morning";
    } else if (time < 20) {
      greeting = "Good day";
    } else {
      greeting = "Good evening";




if else if JavaScript

Multiple if...else statements can be nested to create an else if clause.

if (condition1)
else if (condition2)
else if (condition3)

Do comment if you have any doubts or suggestions on this JS if else topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.