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 clause to execute a statement if
the condition is false.
An if statement looks like this:
if (condition) {
statement1;
} else {
statement2;
}
Here, the condition can be any expression that evaluates to
true or false. (See Boolean
for an explanation of what evaluates to true and false.)
If condition evaluates to true,
statement1 is executed. Otherwise,
statement2 is executed. statement1 and
statement2 can be any statement, including further nested
if statements.
You can also compound the statements using else if to have multiple
conditions tested in sequence, as follows:
if (condition1) {
statement1;
} else if (condition2) {
statement2;
} else if (conditionN) {
statementN;
} else {
statementLast;
}
In the case of multiple conditions, only the first logical condition which evaluates to
true will be executed. To execute multiple statements, group them within a
block statement ({ /* … */ }).
Best practice
In general, it's good practice to always use block statements—especially when
nesting if statements:
if (condition) {
// Statements for when condition is true
// …
} else {
// Statements for when condition is false
// …
}
In general it's good practice to not have an if...else with an assignment like x = y as a condition:
if (x = y) {
// statements here
}
However, in the rare case you find yourself wanting to do something like that, the while documentation has a Using an assignment as a condition section with guidance on a general best-practice syntax you should know about and follow.
Falsy values
The following values evaluate to false (also known as Falsy values):
falseundefinednull0NaN- the empty string (
"")
All other values—including all objects—evaluate to true when passed to a
conditional statement.
Note:
Do not confuse the primitive boolean values
true and false with the true and false values of the
Boolean object!
For example:
const b = new Boolean(false);
if (b) {
// this condition evaluates to true
}
if (b == true) {
// this condition evaluates to false
}
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;
}
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 like this:
switch (expression) {
case label1:
statements1;
break;
case label2:
statements2;
break;
// …
default:
statementsDefault;
}
JavaScript evaluates the above switch statement as follows:
- The program first looks for a
caseclause 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
defaultclause:- If a
defaultclause is found, the program transfers control to that clause, executing the associated statements. - If no
defaultclause is found, the program resumes execution at the statement following the end ofswitch. - (By convention, the
defaultclause is written as the last clause, but it does not need to be so.)
- If a
break statements
The optional break statement associated with each case clause
ensures that the program breaks out of switch once the matched statement is
executed, and then continues execution at the statement following switch.
If break is omitted, the program continues execution inside the
switch statement (and will execute statements under the next case, and so on).
Example
In the following example, if fruitType evaluates to
"Bananas", the program matches the value with case "Bananas"
and executes the associated statement. When break is encountered, the
program exits the switch and continues execution from the statement
following switch. If break were omitted, the statement for
case "Cherries" would also be executed.
switch (fruitType) {
case "Oranges":
console.log("Oranges are $0.59 a pound.");
break;
case "Apples":
console.log("Apples are $0.32 a pound.");
break;
case "Bananas":
console.log("Bananas are $0.48 a pound.");
break;
case "Cherries":
console.log("Cherries are $3.00 a pound.");
break;
case "Mangoes":
console.log("Mangoes are $0.56 a pound.");
break;
case "Papayas":
console.log("Papayas are $2.79 a pound.");
break;
default:
console.log(`Sorry, we are out of ${fruitType}.`);
}
console.log("Is there anything else you'd like?");