JavaScript syntax: Difference between revisions

Content deleted Content added
Operators: add content
Control structures: minor changes and additions
Line 183:
==Control structures==
===If ... else===
if (conditionexpr) {
statements;
} else if (expr) {
statements;
} else {
statements;
}
 
*<code>else</code> statements must be cuddled (i.e. "<code>} else {</code>" , all on the same line), or else some browsers may not parse them correctly.
 
===[[Control_flow#Choice_based_on_specific_constant_values|Switch statement]]===
switch (expressionexpr) {
case label1 VALUE:
statements;
break;
case label2 VALUE:
statements;
break;
default :
statements;
break;
}
 
Actually, *<code>break;</code> is optional.; However leavinghowever, it out isn'ts recommended to use it in most cases, since otherwise code execution will continue to the body of the next <code>case &hellip; :</code> block.
*Add a break statement to the end of the last case as a precautionary measure, in case additional cases are added later.
*Strings can be used for the case values.
*Braces are required.
 
===[[For loop]]===
for ([initial-expression]expr; [condition]cond-expr; [incrementincr-expression]expr) {
statements;
}
 
===For ... in loop===
for (slotvar property-name in object-name) {
This loop goes through all enumerable properties of an object (or elements of an array).
statements involvingusing object-name[slotproperty-name];
 
for (slot in object) {
statements involving object[slot]
}
 
This loop goes*Iterates through all enumerable properties of an object (or elements of an array).
 
===[[While loop]]===
while (conditioncond-expr) {
statements;
}
 
===[[do while loop|Do ... while]]===
do {
statements;
} while (conditioncond-expr);
 
==Functions==