Judgment structure: 1. if conditional branch structure, including if, "if else", "if else if ...", "if else if ... else" statements; 2. "switch case" conditional branch structure; 3. Ternary operation structure, syntax "Condition? Expression 1: Expression 2".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
The branch structure is our common judgment structure, which determines whether to execute certain code programs based on the conditions set by the user. Below, Xiaoqian will introduce to you three common branch structure syntaxes.
if statement
Determine whether to execute the code through an if statementa
Syntax: if (condition) {code to be executed}
Determine whether the code inside {} is executed by whether the condition in () is true
if else statement
Use the if condition to determine which code in {} to execute
Syntax: if (condition) {Execute when the condition is true} else { Executed when the condition is false}
One of the two codes within {} must be executed
if else if ... statement
You can set multiple conditions for judgment through if and else if
Syntax: if (condition 1) {execute when condition 1 is true} else if (condition 2) {condition 2 is true executed}
If the first condition is true, then the content in the following {} will be executed
If the first condition is false, then the second condition will be judged conditions, and so on
For multiple {}, only one will be executed. Once one condition is true, the following conditions will no longer be judged
if else if ... else statement
is basically the same as the previous if else if ..., except that when all conditions are not met, the {} after the last else is executed
is also a type of conditional judgment statement, which is a judgment on a certain variable
Syntax:
Use when you want to judge that a certain variable is equal to a certain value
Example: Display the day of the week based on the number given by the variable
Ternary operation is to use two symbols to form a statement
Ternary operation is just a shorthand form of if else statement
Grammar: Condition? Condition Execute when the condition is true: Execute when the condition is false
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of What judgment structures does JavaScript have?. For more information, please follow other related articles on the PHP Chinese website!