08. BASICS | Conditional Statements in JavaScript
This article will talk about conditional statements in javascript.
if statement
if (condition) {
// execute this code if condition is true
}
if ... else statement
if (condition) {
// execute this code if condition is true
} else {
// execute this code if condition is false
}
if ... else if ... else statement
if (condition1) {
// execute this code if condition1 met
// don't check for remaining conditions
} else if (condition2) {
// execute this code if condition1 doesn't met but condition2 met
// don't check for remaining conditions
} else if (condition3) {
// execute this code if condition1 & 2 don't met but condition3 met
// don't check for remaining conditions
} else if (condition4) {
// execute this code if condition1, 2 & 3 don't met but condition4 met
} else {
// execute this code if none of the above conditions met
}
switch statement
switch (expression) {
case value1:
// Code to execute if expression === value1
break;
case value2:
// Code to execute if expression === value2
break;
// ... more cases ...
default:
// Code to execute if none of the cases match
}
ternary operator
Syntax:
condition ? expression1 : expression2
If
conditionis true, executeexpression1else executeexpression2.
(marks >= 33) ? "Pass" : "Fail"
Happy reading!

