JavaScript if...else
JavaScript if...else
The if/else statement is used for executing a block of code if a specific condition is true. If the condition returns false then another block of code can be executed. It is part of JavaScript Conditional Statements which have a multipurpose use. JavaScript has the following conditional statements:
- if :- to specify a block of code to be executed, if a specific condition is true
- else :- to specify a block of code to be executed, if the same condition is false
- else if :- to specify a new condition to test, if the first condition is false
- switch :- to select one of many blocks of code to be executed
Syntax:-
Its syntax is:-
if (condition) {
// block of code to be executed on true condition
}
OR
if (condition) {
// block of code to be executed on true condition
} else {
// block of code to be executed on false condition
}
OR
if (condition1) {
// block of code to be executed on true condition
} else if (condition2) {
// block of code to be executed on false condition1
} else {
// block of code to be executed on all conditions false
}
Further Explanation:-
Parameter | Description |
---|---|
condition | Required. An expression that evaluates to true or false |