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, includingif
、else if
、else
andswitch
. This article will introduce in detail the syntax, usage and some related precautions of these statements.
if, else if and else
grammar
if
、else if
andelse
The 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,condition1
andcondition2
It 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, ifage
If 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 if
Sentence. 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 toscore
The 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 checkage
Whether it is greater than or equal to 18, if so, further checkhasID
Whether it is true. If both conditions are met, the console will output "You can enter the bar"; otherwise, ifhasID
As false, the console will output "You need to bring your ID card"; ifage
If it is less than 18, the console will output "You are underage and cannot enter the bar".
switch
grammar
switch
The 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,expression
is an expression used with eachcase
Compare the values of .
usage
switch
Statements 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 today
The value of , the console will output the corresponding day of the week.
Things to note
Using break
existswitch
In the statement, usebreak
Statements 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 executeswitch
The end of the statement.
Use default
default
Statements are optional, but they can help you deal with those that don't match any onecase
Scenario. For example, in the above example, ifday
The value of is not any of Monday to Sunday, and the console will output "Unknown Date".
in conclusion
if
、else if
、else
andswitch
It 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!