English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript if...else statement

 JavaScript Statements and Variable Declarations

if...elseThe statement is one of the JavaScript conditional statements, used to execute different operations based on different conditions.

In JavaScript, we have the following conditional statements:

  • ifIf the specified condition is true, useifTo specify the code block to be executed

  • If the same condition is false, useelseTo specify the code block to be executed

  • If the first condition is false, useelse ifSpecify a new condition to be tested

  • UsingswitchChoose one of many code blocks to be executed

Syntax:

ifIf the condition is trueifStatement specifies the code block to be executed:

if (condition) {
 //The code block to be executed if the condition is true
}

elseStatement specifies the code block to be executed if the condition is false:

if (condition) {
    //The code block to be executed if the condition is true
} else {
   //The code block to be executed if the condition is false
}

eelse ifStatement specifies a new condition, if the first condition is false:

if (condition1) {
   //If the condition1The code block to be executed if true
} else if (condition2) {
   //If the condition1Is false while condition2The code block to be executed if true
} else {
   //If the condition1and condition2The code block to be executed if false
}
var x = -4;
if (x < 0) {
   document.getElementById("result").innerHTML = "NEGATIVE";
}
Test and see‹/›

Browser compatibility

All browsers fully support if ... else statements:

Statement
if...elseIsIsIsIsIs

Parameter value

ParameterDescription
conditionAn expression that evaluates to true or false

Technical details

JavaScript version:ECMAScript 1

More examples

If the value of variable x is less than 0, output "NEGATIVE", otherwise output "POSITIVE":

var x = -4;
if (x < 0) {
   msg = "NEGATIVE";
} else {
   msg = "POSITIVE";   
}
Test and see‹/›

1010""20, then write " x is2"0", otherwise write " x does not exist"

var x = 20;
if (x == 10) {
   document.write("x is 10;
} 20) {
   document.write("x is 20");
} else {
   document.write("x does not exist");
}
Test and see‹/›

You can use multipleelse ifstatement:

// Set the student's current grade
var grade = 88;
//Check if the grade is A, B, C, D, or F
if (grade >= 90) {
   document.write("A");
} else if (grade >= 80) {
   document.write("B");
} else if (grade >= 70) {
   document.write("C");
} else if (grade >= 60) {
   document.write("D");
} else {
   document.write("F");
}
Test and see‹/›

You can write a one-line statement without braces:

var x = -4;
if (x < 0)
   msg = "NEGATIVE";
else
   msg = "POSITIVE";
Test and see‹/›

If the user clicks the image, change the value of the src attribute of the image:

<img id="demo" onclick="changeImage()" src="avatar-female.jpg">
<script>
function changeImage() {
   var image = document.getElementById("demo");
   if (image.src.match("female")) {
   image.src = "avatar-male.jpg";
   } else {
   image.src = "avatar-female.jpg";
   }
}
</script>
Test and see‹/›

Use if ... else statements to verify input data:

function myFunc(x) {
   var text;
//if x is not a number, or less than10, or greater than20, output "Input not valid"
//if x is10to2between 0 and, then output "Input OK"
   if (isNaN(x) || x < 10 || x > 20) {
  text = "Input not valid";
   } else {
  text = "Input OK";
   }
document.getElementById("result").innerHTML = text;
}
Test and see‹/›

Nested if...else statements:

var a = 10, b = 20, c = 30;
var answer;
if (a > b) {
   if (a > c) {
  answer = "A is the greatest among three";
   } else {
  answer = "C is the greatest among three";
   }
} else if (b > c) {
   answer = "B is the greatest among three";
} else {
   answer = "C is the greatest among three";   
}
Test and see‹/›

You can also refer to

JavaScript Tutorial:JavaScript If...Else Statement

JavaScript Tutorial:JavaScript switch

 JavaScript Statements and Variable Declarations