When it comes to the branching structure in JavaScript, we have to mention the word Process control, and all of our programs are made up of data and algorithms.
program = data + algorithm
Usually we call the algorithm can be done by "order", "branch", "loop" three kinds of structure to complete the combination.
In the ECMA, some statements (also called Process Control statements, branch structure statements) are defined, essentially defining the primary syntax in ECMAScript, which typically uses one or more keywords to accomplish a given task.
1.1 If statement
If statement-use this statement to execute code only if the specified condition is true
1 if (condition) 2 {3 true when executing code 4 }
If...else Statement-executes code when the condition is true and executes other code when the condition is false
1 if (condition) 2 {3 true when executing code 4 }5Else 6 {7 true when executing code 8 }
If...else If....else Statement-Use this statement to select one of several code blocks to execute
1 if(Condition 1)2 {3When condition 1 istruecode to execute when4 }5 Else if(Condition 2)6 {7When Condition 2 istruecode to execute when8 }9 ElseTen { OneWhen condition 1 and condition 2 are nottruecode to execute when A}
1.2 Switch statement
Use the switch statement to select one of several code blocks to execute.
1 Switch(n)2 {3 Case1:4Executing code block 15 Break;6 Case2:7Executing code block 28 Break;9 default:TenN and Case1 and Case2code that executes at different time One}
How it works: first set an expression n (usually a variable). The value of the subsequent expression is compared to the value of each case in the structure. If there is a match, the code block associated with the case is executed. Use break to prevent your code from automatically running down a case.
Default keyword
Use the default keyword to specify what to do when a match does not exist:
1 varday=NewDate (). GetDay ();2 Switch(Day)3 {4 Case6:5x= "Today it ' s Saturday";6 Break;7 Case0:8x= "Today it ' s Sunday";9 Break;Ten default: OneX= "Looking forward to the Weekend"; A}
Explanation: Today is not a code snippet that will be executed in Saturday or Sunday.
Comparison of 1.3 if and switch
Switch Case Else if Switch Case only for conditions that are equal to comparison Else if any condition available if Else if () with implicit conversion, the condition is converted to a Boolean efficiency slightly lower than the equal comparison of the implicit conversion of the switch case, Slightly higher efficiency
Branching structure in JavaScript