In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
What this article shares with you is about the branch structure of JavaScript. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article. Let's take a look at it with the editor.
Our js code is executed sequentially (from top to bottom)
The logical branch is to decide whether or not to execute some code according to the conditions we have set.
IF conditional branch structure if statement
An if statement is used to determine whether the code is executed or not
Syntax: if (condition) {Code to be executed}
Determine whether the code in {} is executed by whether the condition in () is true or not.
/ / execute the code if (true) {in {} when the condition is true
Alert ('because the condition is true, I will execute')} / / do not execute the code if (false) {in {} when the condition is false
Alert ('because the condition is false, I will not execute')}
If else statement
Decide which {} code to execute according to the if condition
Syntax: if (condition) {execute when condition is true} else {execute when condition is false}
One of the code within the two {} must be executed
/ / if the condition is true, the {} if (true) {after if will be executed.
Alert ('because the condition is true, I will execute')} else {
Alert ('because the condition is true, I will not execute')} / / when the condition is false, the {} if (false) {after else will be executed.
Alert ('I will not execute because the condition is false')} else {
Alert ('because the condition is false, I will execute')}
If else if... Statement
You can use if and else if to set multiple conditions to judge
Syntax: if (condition 1) {execute when condition 1 is true} else if (condition 2) {execute when condition 2 is true}
The conditions will be judged from the beginning.
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 is judged, and so on.
Of multiple {}, only one will be executed. Once there is a condition of true, the latter will not be judged.
/ / the first condition is true, and the second condition is false, which will eventually print "I am code snippet 1" if (true) {
Alert ('I am snippet 1')} else if (false) {
Alert ('I am snippet 2')} / / the first condition is true, the second condition is true, and eventually "I am snippet 1" / / will be printed because as long as the previous condition is met, it will not continue to judge if (true) {
Alert ('I am snippet 1')} else if (true) {
Alert ('I am code snippet 2')} / / the first condition is false, the second condition is true, and the final print "I am code snippet 2" / / will continue to judge if (false) {only if the previous condition is false.
Alert ('I am snippet 1')} else if (true) {
Alert ('I am code snippet 2')} / / the first condition is false, the second condition is false, and ultimately nothing will happen / / because when all the conditions are false, the code in the two {} will not execute if (false) {
Alert ('I am snippet 1')} else if (false) {
Alert ('I am code snippet 2')}
If else if... Else statement
And the previous if else if. Basically the same, but when all the conditions are not met, execute the {} after the last else
/ / the first condition is false, the second condition is false, and eventually "I am snippet 3" / / the code in the {} after else will be executed only if all the previous conditions are not met / / as long as the previous condition is satisfied, then if (false) {will not be executed.
Alert ('I am snippet 1')} else if (false) {
Alert ('I am code snippet 2')} else {
Alert ('I am code snippet 3')}
SWITCH conditional branching structure
It is also a kind of conditional judgment sentence.
It's the judgment of a certain variable.
Syntax:
Switch (variable to be judged) {
Case situation 1:
Case 1 Code to be executed
Break
Case situation 2:
Case 2 Code to be executed
Break
Case situation 3:
Case 3 Code to be executed
Break
Default:
Code executed when none of the above is satisfied.
To judge that a variable is equal to a certain value to use
Example: the number given according to the variable shows what day it is
Var week = 1switch (week) {
Case 1:
Alert ('Monday')
Break
Case 2:
Alert ('Tuesday')
Break
Case 3:
Alert ('Wednesday')
Break
Case 4:
Alert ('Thursday')
Break
Case 5:
Alert ('Friday')
Break
Case 6:
Alert ('Saturday')
Break
Case 7:
Alert ('Sunday')
Break
Default:
Alert ('Please enter a number between 1 and 7')}
Ternary operation (extension)
Ternary operation is to use two symbols to form a statement.
Ternary operation is just an abbreviated form of if else statement.
Grammar: condition? Execute when condition is true: execute when condition is false
Var age = 18? Alert ('adult'): alert ('underage')
The above is what the branch structure of JavaScript is, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.