In general, when writing code, you always need to perform different actions for different decisions. You can do this using conditional statements in your code. In JavaScript, we can use the following conditional statements:

  • If statement – This statement is used to execute code only if the specified condition is true
  • if… Else statement – Executes code when the condition is true and other code when the condition is false
  • if… else if…. Else statement – Use this statement to select one of multiple code blocks to execute
  • Switch statement – Use this statement to select one of multiple code blocks to execute

If statement

This statement executes code only if the specified condition is true

if(condition) {when the condition istrueThe code executed when}Copy the code
var time =10
if (time<20) {console.log("Good day");
}
Copy the code

Omit the {} pattern

function search(num) {
  if (num>90) return "Excellent grades"
  return "Not a good grade."
}
console.log(search(100));


var time =10
if (time<20) console.log("Good day");
Copy the code

if… Else statements

Please use the if… The else statement executes code when the condition is true and other code when the condition is false

if(condition) {when the condition istrueThe code executed when}else{when the condition is nottrueThe code executed when}Copy the code
if (1 > 2) {
  console.log("Condition held")}else {
  console.log("The condition does not hold.")}if (1 > 2) console.log("Condition held");
else console.log("The condition does not hold.");
Copy the code

if… else if… Else statements

Use the if… else if… Else statement to select one of multiple code blocks to execute

ifCondition1 {condition11trueThe code executed when}else if(condition2) {condition22trueThe code executed when}else{when the condition1And conditions2Don't fortrueThe code executed when}Copy the code
var time = 8;
if (time<10) {
    console.log("Good morning.");
} else if (time>=12 && time<14) {
    console.log("Good afternoon.");
} else if (time>=14 && time<18) {
    console.log("Good afternoon");
} else {
    console.log("Good evening!");
}



var time = 12;
if (time<12) console.log("Good morning.");
else if (time>=12 && time<14)console.log("Good afternoon."); 
else if (time>=14 && time<18)console.log("Good afternoon"); 
else  console.log("Good evening!");
Copy the code

A switch statement

Use the switch statement to select one of the multiple code blocks to execute

switch(n) {
    case 1: Executes a code block1
        break;
    case 2: Executes a code block2
        break;
    defaultAnd:case 1case 2Non-simultaneous execution of code}Copy the code
var d=new Date().getDay(); 
switch (d) { 
  case 0: console.log("Today is Sunday."); break; 
  case 1: console.log("It's Monday."); break; 
  case 2: console.log("Today is Tuesday."); break; 
  case 3: console.log("It's Wednesday."); break; 
  case 4: console.log("It's Thursday."); break; 
  case 5: console.log("It's Friday."); break; 
  case 6: console.log("It's Saturday."); break; 
}

/* Today is Wednesday */
Copy the code
<! DOCTYPE html><html lang="en">
<head>
  <title>Simple use of switch</title>
</head>
</html>

<script>
  var str = prompt("Input value")
  switch (str) {
    case "A": alert("Good"); break;
    case "B": alert("Good"); break;
    case "C": alert("In"); break;
    case "D": alert("Qualified"); break;
    default: alert("Fail"); break;
  }
</script>
Copy the code

The default keyword

var d=new Date().getDay();
switch (d) {
    case 6:x="It's Saturday."; break;
    case 0:x="Today is Sunday."; break;
    case 3:x="Today is Sunday."; break;
    default: x="Looking forward to the weekend";
}
document.getElementById("demo").innerHTML=x;
Copy the code

Lack of break

If a break is missing after a correct option statement, the next statement will run, whether true or false, until it encounters a break statement and then stops

var d=3 || new Date().getDay();  //d runs 3
switch (d) { 
  case 0: console.log("Today is Sunday."); 
  case 1: console.log("It's Monday."); 
  case 2: console.log("Today is Tuesday."); 
  case 3: console.log("It's Wednesday."); 
  case 4: console.log("It's Thursday.");
  case 5: console.log("It's Friday."); break; 
  case 6: console.log("It's Saturday."); 
}

Today is Wednesday. Today is Thursday. Today is Friday
Copy the code