JavaScript Conditions and Branching Code Structures

By Andy Harris

Part of JavaScript & AJAX For Dummies Cheat Sheet

Look to the following table for JavaScript control structures you can use in your program code to add branching and looping behavior to your JavaScript programs.

Element Description
if (condition){
// content
} else {
// more content
} // end if
Executes content only if condition is true.
Optional else clause occurs if condition
is false.
switch (expression)
case: value;
//code
break;
default:
//code
}
Compares expression against one or more values. If expression
is equal to value, runs corresponding code.
Default clause catches any uncaught values.
for(i = 0; i < count; i++)
//code
} // end for
Repeats code i times.
While (condition){
//code
} // end while
Repeats code as long as condition is true.
Function fnName(paramaters) {
//code
} // end function
Defines a function named fnName and
sends it parameters. All code inside the function will execute when
the function is called.