When used inside of a function, the var statement defines the function's private variables. Statements tend to be executed in order from top to bottom. The sequence of execution can alttered by Conditional, Looping, and Distruptive Statements.
if and switch
while, for, and do
break, return, and throw
The switch statement performs a multiway branch. It compares the expression for equality with all of the specified cases. The expression can produce a number or a string. When an exact match is found, the statement of the matching case clause are executed. If there is no match, the optional default statements are executed.
A case clause contains one or more case expressions. The case expressions need not be constants. The statement following a clause should be a disruptive statement to prevent fall through into the next case. The break statement can be used to exit from a switch.
The while statement performs a simple loop. If the expression is falsy, then the loop will break. While the expression is truthy, the block will be executed.
The for statement is a more complicated looping statement. It comes in two forms.
The conventional form is controlled by three optional clauses: the initialization, the condition, and the increment. First, the initialization is done, which typically initializes the loop variable. Then, the condition is evaluated. Typically, this tests the loop variable against a completion criterion. If the condition is omitted, then a condition of true is assumed. If the condition is falsy, the loop breaks. Otherwise, the block is executed, then the increment executes, and then the loop repeats with the condition.
The other form (called for in) enumerates the property names (or keys) of an object. On each iteration, another property name string from the object is assigned to the variable.
It is usuall necessary to test object.hasOwnProperty(variable) to determine whether the property name is truly a member of the object or was found instead on the prototype chain.
for (myvar in obj) {
if (obj.hasownProperty(myvar)) {
...
}
}
The do statement is like the while statement except that the expression is tested after the block is executed instead of before. That means that the block will always be executed at least once.