if Statements

By: Dusty Arlia
Published on October 24, 2012
Last Updated on Saturday, July 11, 2015 at 4:08 PM
Total Updates: 6

if statements, are programming statements that ask a simple yes or no question that's called a condition.

Here is the basic syntax for an if statement:

if (/*condition*/) {
//statements
}

Conditions may also be called Boolean conditions because there is only two possible values they can evaluate to: true or false. The JavaScript interpreter evaluates the condition in parentheses after the if keyword. If the answer to the question is yes (or true) the next statement is executed. You can remember this by the saying "true, do". If the answer to the condition is no (or false), the next statement is skipped.

The statement that follows the condition can be a single primitive statement or a compound statement (which is multiple statements in between curly braces). It is preferred that you use curly braces ({}) like this:

if (3 < 2) {
alert("This alert is skipped over");
} 
else {
alert("This is the alert that pops up!");
}

But this works too:

if (3 < 2)
alert("This alert is skipped over");
alert("This is the alert that pops up!");

But having multiple statements with no curly braces can return a syntax error if you do it the wrong way. Here is an example of an if statement that causes a syntax error:

if (3 < 2)
alert("This alert is skipped over");
alert("This statement creates your syntax error");
else
alert("The alert message you expect to receive, but don't.")

In this example, the JavaScript interpreter expects only one statement before the else clause. To fix this if statement, enclose the two branches with curly braces.

If the condition evaluates to false, the program jumps over the curly braces immediately following it to either run the else if statement, else statement, or if there is no else if or else statements, then the next line of code.

if statements can make your program more dynamic. They give your program the ability to make decisions based on different conditions. They are just one example of a couple different types of conditional statements. if statements are one of the most commonly used conditional statements, but they have a couple different names and variations:

How It Works:

After the if keyword comes the condition. The JavaScript interpreter evaluates the condition in parentheses, and determines whether the condition is true or false. Depending on whether the answer is a yes or a no (true or false are the actual Boolean values) will decide which part of your program will run next. If the condition is true, the statements following the condition in the curly braces ({}) are executed. If the condition evaluates to false, the program jumps to the next branch of code - either in the else if block or else block (both are optional), or if there is no else if or else branch, then whatever code follows the first pair of curly braces.

Why Use if Statments:

if statements give your program the ability to make decisions based on conditions. Here's an example:

if (are_you_logged_in == "yes") {
//display welcome screen
} 
else {
//display login screen
}

This example shows how you can display a custom page based on whether or not your visitor is logged in. There is no technical details, but you can understand the logic. Having your JavaScript program make decisions allows your visitors to receive custom pages. This decision-making characteristic of the JavaScript language, is what makes JavaScript so powerful.

Comments:

Ad: