consists of zero or more expressions enclosed in curly brackets { and };
they can be nested within each other indefinitely.
{ var a = 1; { var b = a + 2; { var c = b * 3; } } }
- Use end-of-line semicolons, although it is optional when you have one expression per line
- Indent any code placed within curly brackets
- Use curly brackets, although they are optional when you have a block of only one expression
if (condition) statement1 else statement2if (condition1) statement1 else if (condition2) statement2 else statement3Use code blocks for control statements, even when it is only one expression.
if (test) console.log('hello!'); //valid, but error-prone and should be avoided if (test) { //preferred alert(test); }
switch (expression) {
case value1: statement1; break;
case value2: statement2; break;
//...
case valueN: statementN; break;
default: statement
}The step-by-step procedure for executing a switch statement is as follows:
- evaluate the switch
expressionfound in parentheses, remember it - move to the first case, compare its value with the one from step 1
- if the comparison in step 2 returns
true, execute the code in thecaseblock - after the
caseblock is executed, if there's abreakstatement at the end of it, exit theswitch - if there's no
breakor step 2 returnedfalse, move on to the nextcaseblock, repeat steps 2 to 5 - if we're still here (we didn't exit in step 4), execute the code following the
defaultstatement.
- the
switchstatement works with all data types (in many languages it works only with numbers)- the
casevalues need not be constants, they can be variables and even expressions- the comparison of value is done using the identically equal operator, so no type coercion occurs
The
switchstatement prevents writing something like this:if (i == 25) { console.log("25"); } else if (i == 35) { console.log("35"); } else if (i == 45) { console.log("45"); } else { console.log("other"); }The equivalent
switchstatement is as follows:switch (i) { case 25: console.log("25"); break; case 35: console.log("35"); break; case 45: console.log("45"); break; default: console.log("other"); }
Put a
breakstatement after eachcaseto avoid having cases fall through into the next one;
if you need a case statement to fall through, include a comment indicating the intentional omission.switch (i) { case 25: // falls through case 35: console.log("25 or 35"); break; case 45: console.log("45"); break; default: console.log("other"); }
The do-while statement is a post-test loop:
the escape condition is evaluated only after the code inside the loop has been executed;
the body of the loop is always executed at least once before the expression is evaluated.
do {
statement
} while (expression);var i = 0; do { i += 1; } while (i < 10); console.log(i); //10
The while statement is a pre-test loop:
the escape condition is evaluated before the code inside the loop has been executed;
it is possible that the body of the loop is never executed.
while (expression) statementvar i = 0; while (i < 10) { i += 2; }
The for statement is a pre-test loop with code before entering the loop and post-loop code.
for (initialization; expression; post-loop-expression) statementThe
forloops can be nested within each other.var s = '\n'; var n = 3; var m = 3; for (var i = 0; i < n; i++) { for (var j = 0; j < m; j++) { s += '* '; } s += '\n'; } console.log(s); " * * * * * * * * * "
The initialization, control expression, and postloop expression are all optional.
You can create an infinite loop by omitting all three, like this:for (;;) { //infinite loop doSomething(); }
The
forloop can be turns into a while loop including only the control expression:var count = 10; var i = 0; for (; i < count; ) { console.log(i); i++; }
The for in statement is a strict iterative statement used to enumerate the properties of an object.
for (property in expression) statementvar obj = {a: 1, b: 2, c: 3}; var s = ''; for (var key in obj) { s += key + ' '; } console.log(s); //"a b c ";