if-else Construct & Condition Statements

Main Page | Session 11 Index | Data Types & Variables | Browser Detection


if-else Construct

An important part of any programming language is the ability to make decisions, or choices. The if-else construct is the most basic and common method of decision making.

The syntax of the if-else construct is:

if (<condition>) <action-one> else <action-two>

where:

condition
is a logical condition which evaluates to either true or false
action-one
is the set of JavaScript commands to perform if the condition evaluates true
action-two
is the set of JavaScript commands to perform if the condition evaluates false

NOTES:


Examples:

if-else Example One

This example will use the following if statement to decide if the number entered is equal to 10. If the number is equal to 10, the background colour of this document will change to green, otherwise it will change/stay red.

The function that performs this test is:

function example1() { var response=prompt("Enter a number: ",""); if (response==10) document.bgColor="#66FF66"; else document.bgColor="#FF0000"; }

Click Here to start example one.

Set background to white


if-else Example Two

This example will use the following 'if' statement to decide if the number entered is equal to 10. If the number is equal to 10, the background colour of this document will change to green, otherwise it will change/stay red. In both cases, an alert box is displayed to inform the user of the results of the test. This example illustrates the use of multiple lines of code for the action of an if-else branch.

function example2() { var response=prompt("Enter a number: ",""); if (response==10) { document.bgColor="#66FF66"; var msg="\nYou matched the right number!"; } else { document.bgColor="#FF0000"; var msg="\nSorry, the numbers didn't match"; } alert(msg); }

Click Here to start example two.

Set background to white


if-else Example Three

This example will use the following 'if' statement to decide if the number entered is equal to 10 and the user entered their name. If both conditions are true, the background colour of this document will change to green, otherwise it will change/stay red. In both cases, an alert box is displayed to inform the user of the results of the test. This example illustrates the use of multiple lines of code for the action of an if-else branch and a compound condition.

function example3() { var name=prompt("Enter your name: ",""); var response=prompt("Enter a number: ",""); if ((response==10) && (name !="")) { document.bgColor="#66FF66"; var msg="\nYou've met all the conditions!"; } else { document.bgColor="#FF0000"; var msg="\nSorry, you appear to have missed something."; } alert(msg); }

Click Here to start example three.

Set background to white


Condition Statements

When the results of a condition test is to be one of two possible values, a condition statement may be more practical than an if-else construct. The condition statement has the following syntax:

(<condition>) ? <true-value> : <false-value>;

where:

condition
is a logical condition which evaluates to either true or false
true-value
is the value to return if the condition evaluates true
false-value
is the value to return if the condition evaluates false

Condition statements may be used wherever a value is required, such as in an assignment statement or a return statement. For example:

The following code sets the value of status depending upon the age entered by the user.

function example4() { var age=prompt("Enter your age: ",""); var status=(age >= 19) ? "adult" : "minor"; alert("You are a " + status); }

Click Here to start condition statement example.


Main Page | Session 11 Index | Data Types & Variables | Browser Detection