Skip to content

Instantly share code, notes, and snippets.

@sbaldwin24
Last active August 29, 2015 14:07
Show Gist options
  • Save sbaldwin24/4c95ed62132a5323d7c0 to your computer and use it in GitHub Desktop.
Save sbaldwin24/4c95ed62132a5323d7c0 to your computer and use it in GitHub Desktop.
Statements

Statemenets

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.

Coniditional Statements

if and switch

Looping Statements

while, for, and do

Disruptive Statements

break, return, and throw

Switch Statement

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.

Case Clause

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.

While Statement

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.

For Statement

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)) {
    ...
  }
}

Do Statement

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment