Contents: Click for quick access
Conditional Statements
Conditional statements are used to do various actions in response to various conditions ( weather condition is true or false.)
In JavaScript, there are three types of if statements.
- If Statement
- If else statement
- if else if statement
- If Statement: An if statement directs the program to adopt one of two paths, based on the outcome of an expression evaluation.
if (true) {
//action perform
}
if (false) {
//action perform (? never ?)
}
- If else statement: It determines if the condition is true or false by evaluating the content.
if (true) {
//action perform
} else {
//action perform else
}
- if else if statement: It specify a new condition to be tested, if the first condition is false.
if (condition1) {
// if condition1 is true
} else if (condition2) {
// if the condition1 is false and condition2 is true
} else {
// if the condition1 is false and condition2 is false
}
JavaScript Switch Statement
JavaScript Switch Statement: It’s performed to carry out a variety of tasks depending on the situation.
Syntax
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
If a match is found, the associated code block is run otherwise
If no match is found, the default code block is performed.