Controls the flow of code that runs sequentially from top to bottom.
Control statements make the flow of code difficult to understand, which detracts from readability.
Should suppress the use of control statements with using high order functions (map, filter, reduce, forEach)
Block Statement / Compound Statement
0 or more statements enclosed in curly braces
{}
In other languages, a block creates a scope, but JavaScript creates a function-level scope. Therefore, variables created in block are global variables.
Block statements (code blocks) are treated as one execution unit.
A semicolon (;) is usually appended to the end of the statement, but no semicolon at the end of the block statement.
Conditional Statement
Determine whether to execute a code block based on the evaluation result of a given conditional expression.
Conditional expression
Expression that can be evaluated as boolean value
JavaScript provides 2 conditional statements as if..else statements and switch statement
if … else Statement
1 | if (Conditional expression) { |
Most if … else statement can be replaced with the ternary conditional operator discussed in 6. Operators.
switch Statement
1 | switch (Expression) { |
- If the code can be represented by if…else, the use of the switch should be avoided whenever possible.
- Evaluates a given conditional expression and moves the execution order to a case statement with an expression that matches the value.
- Used when there are more cases than if…else statements.
- If do not use a break statement in a case, all the cases are checked sequentially to the default due to the fall through property of the switch.
Loop Statement
Loop statement executes the code block repeatedly until the conditional expression is false.
for Statement
The for statement executes the code block repeatedly until the conditional expression is determined to be false.
1 | for (declaration statement or assignment statement; conditional expression; Increase / decrease expression){ |
1 | for (var i = 0; i < 2; i++) { |
while Statement
The while statement repeatedly executes the code block repeatedly if the evaluation result of the given conditional expression is true.
1 | while (conditional expression){ |
do while Statement
It’s the same as while, but once it runs and checks the conditional
break Statement
The break statement exits the code block.
Actually it escapes the label statement, the for loop (for, for … in, for … of, while, do … while) or the code block of the switch statement.
If a break statement is used in addition to the label statement, the loop statement, or the code block of the switch statement, SyntaxError occurs.
continue Statement
- If continue is encountered, the code will stop running and move to the increment or decrement statement of the loop
- The statements under continue are not executed.