1. if

The if statement evaluates an expression and, based on the results, determines which code, if any, executes within a program. More complex if statements can control which code executes based on multiple conditions. It can control how a program acts depending on whether a variable contains a certain value or a form field is filled in correctly.

The basic structure of an if statement is:

if (some condition) {
// do something here

The if statement examines the validity, or truthfulness, of a condition to determine whether the code within the conditional (inside the braces) is to be executed. The condition is a Boolean expression that, when evaluated to true, causes the if statement to execute the code in the conditional.

You can negate an expression in the condition to cause the code to run if the expression evaluates to false. For example,

if (! some condition) {
// do something here

In this case, the condition starts with the negation operator, which means that the condition would need to evaluate to false for the code inside the conditional to execute.

Compound Conditions

Often, you need to test for more than one condition within the same if statement. For example,

if ((inputNum < 51 ) || (inputNum > 99)) {
alert("That number, " + inputNum + ", is not between 50 and 100.");

The above statement the logical OR operator and reads, "If inputNum is less than 51 or inputNum is greater than 99, do this".

2. else if / else

Using else if and else, you can create multiple levels of conditions, each of which is tested in turn. The code within the first matching condition is executed. If nothing matches, the code inside the else condition, if present, is executed.

3. switch

The switch statement is an easy and efficient way to test a variable for several values and then execute code based on whichever case matches. Although you can accomplish the task by using if/else if statements, doing so can be cumbersome. The switch statement is more useful for this situation.