CONDITIONAL STATEMENTS :]
A conditional statement is a set of commands that executes if a specified condition is true. JavaScript supports two conditional statements: if...else
and switch
.
if...else Statement
Use the if
statement to execute a statement if a logical condition is true. Use the optional else
if
statement looks as follows: clause to execute a statement if the condition is false. An
if (condition)
statement_1
[else
statement_2]
condition
can be any expression that evaluates to true or false. If condition
evaluates to true, statement_1
is executed; otherwise, statement_2
is executed. statement_1
and statement_2
can be any statement, including further nested if
statements.
You may also compound the statements using else if
to have multiple conditions tested in sequence, as follows:
if (condition)
statement_1
[else if (condition_2)
statement_2]
...
[else if (condition_n_1)
statement_n_1]
[else
statement_n]
To execute multiple statements, use a block statement ({ ... }
) to group those statements. In general, it is a good practice to always use block statements, especially in code involving nested if
statements:
if (condition) {
statements_1
} else {
statements_2
}
It is advisable to not use simple assignments in a conditional expression, because the assignment can be confused with equality when glancing over the code. For example, do not use the following code:
if (x = y) {
/* do the right thing */
}
If you need to use an assignment in a conditional expression, a common practice is to put additional parentheses around the assignment. For example:
if ((x = y)) {
/* do the right thing */
}
Do not confuse the primitive boolean values true
and false
with the true and false values of the Boolean object. Any value that is not undefined
, null
, 0
, NaN
, or the empty string (""
), and any object, including a Boolean object whose value is false, evaluates to true when passed to a conditional statement. For example:
var b = new Boolean(false);
if (b) // this condition evaluates to true
Example
In the following example, the function checkData
returns true if the number of characters in a Text
object is three; otherwise, it displays an alert and returns false.
function checkData() {
if (document.form1.threeChar.value.length == 3) {
return true;
} else {
alert("Enter exactly three characters. " +
document.form1.threeChar.value + " is not valid.");
return false;
}
}
switch Statement
A switch
statement allows a program to evaluate an expression and attempt to match the expression's value to a case label. If a match is found, the program executes the associated statement. A switch
statement looks as follows:
switch (expression) {
case label_1:
statements_1
[break;]
case label_2:
statements_2
[break;]
...
default:
statements_def
[break;]
}
The program first looks for a case
clause with a label matching the value of expression and then transfers control to that clause, executing the associated statements. If no matching label is found, the program looks for the optional default
clause, and if found, transfers control to that clause, executing the associated statements. If no default
clause is found, the program continues execution at the statement following the end of switch
. By convention, the default
clause is the last clause, but it does not need to be so.
The optional break
statement associated with each case
clause ensures that the program breaks out of switch
once the matched statement is executed and continues execution at the statement following switch. If break
is omitted, the program continues execution at the next statement in the switch
statement.