SoFunction
Updated on 2025-04-14

Syntax, usage and precautions for if, else if, else and switch in JavaScript

Write in front

In programming, conditional judgment is an important means to control program flow. JavaScript provides a variety of ways to make conditional judgments, includingifelse ifelseandswitch. This article will introduce in detail the syntax, usage and some related precautions of these statements.

if, else if and else

grammar

ifelse ifandelseThe basic syntax of a statement is as follows:

if (condition1) {
  // If condition1 is true, execute the code here} else if (condition2) {
  // If condition1 is false and condition2 is true, execute the code here} else {
  // If both condition1 and condition2 are false, execute the code here}

in,condition1andcondition2It is an expression used to determine whether the subsequent code block is executed.

usage

Single condition

The easiest way to use a single condition to control the execution of the code. For example:

const age = 18;
if (age >= 18) {
  ("You're already an adult!");
}

In this example, ifageIf the value is greater than or equal to 18, the console will output "You are already an adult!".

Multiple conditions

If you need to decide whether to execute a certain piece of code based on multiple conditions, you can useelse ifSentence. For example:

const score = 85;
if (score >= 90) {
  ("excellent");
} else if (score >= 80) {
  ("good");
} else if (score >= 70) {
  ("medium");
} else {
  ("It takes effort");
}

In this example, according toscoreThe console will output the corresponding evaluation.

Nesting conditions

Sometimes, you may need to nest another conditional statement in one conditional statement. For example:

const age = 18;
const hasID = true;
if (age >= 18) {
  if (hasID) {
    ("You can enter the bar");
  } else {
    ("You need to bring your ID card");
  }
} else {
  ("You are underage and cannot enter the bar");
}

In this example, first checkageWhether it is greater than or equal to 18, if so, further checkhasIDWhether it is true. If both conditions are met, the console will output "You can enter the bar"; otherwise, ifhasIDAs false, the console will output "You need to bring your ID card"; ifageIf it is less than 18, the console will output "You are underage and cannot enter the bar".

switch

grammar

switchThe basic syntax of a statement is as follows:

switch (expression) {
  case value1:
    // If expression is equal to value1, execute the code here    break;
  case value2:
    // If expression is equal to value2, execute the code here    break;
 ...
  default:
    // If expression is not equal to the value of any case, execute the code here}

in,expressionis an expression used with eachcaseCompare the values ​​of  .

usage

switchStatements are usually used to process multiple possible values. For example:

const day = "Monday";
switch (day) {
  case "Monday":
    ("Today is Monday");
    break;
  case "Tuesday":
    ("Today is Tuesday");
    break;
  case "Wednesday":
    ("Today is Wednesday");
    break;
  case "Thursday":
    ("Today is Thursday");
    break;
  case "Friday":
    ("Today is Friday");
    break;
  case "Saturday":
    ("Today is Saturday");
    break;
  case "Sunday":
    ("Today is Sunday");
    break;
  default:
    ("Unknown date");
}

In this example, according todayThe value of  , the console will output the corresponding day of the week.

Things to note

Using break

existswitchIn the statement, usebreakStatements can prevent the code from continuing to execute to the nextcase. If you don't use itbreak, then once a matching one is foundcase, the code will continue to executeswitchThe end of the statement.

Use default

defaultStatements are optional, but they can help you deal with those that don't match any onecaseScenario. For example, in the above example, ifdayThe value of  is not any of Monday to Sunday, and the console will output "Unknown Date".

in conclusion

ifelse ifelseandswitchIt is the most commonly used conditional judgment statement in JavaScript. By using these statements reasonably, you can execute different code blocks according to different conditions. Remembering best practices such as using strict equality operators, avoiding spaces and newlines, using brackets, etc. can help you write clearer and more reliable code.

This is the article about the syntax, usage and precautions of if, else if, else and switch in JavaScript. For more information about the usage of if, else if, else if, else and switch in JS, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!