Conditional Statements in JavaScript (If else, switch, and Ternary Operator)
Conditional Statements (If else)
Conditional statements are the backbone of controlling program flow in JavaScript. They allow your code to make decisions based on various conditions, leading to different outcomes. The main types of conditional statements in JavaScript are:
1. if
Statement
It is the most basic conditional statement in JavaScript. It checks a condition
and only executes the code block within its curly braces {}
if the condition is true
.
For Example:
let age = 18;
if (age >= 18) {
console.log("You are eligible to apply!");
}
Output: You are eligible to apply!
2. else
Statement
What does it print when the if
statement has gone wrong? Here we write else
a statement for alternative conditions. In simple words if
provides an alternative code block when the if
condition is false
.
For Example:
let age = 16;
if (age >= 18) {
console.log("You are eligible to apply!");
}else{
console.log("You are eligible not to apply!");
}
Output: You are eligible not to apply!
3. else if
Statement
else if
allows chaining multiple conditions after an if
. Each else if
checks a new condition and executes its code block if that condition is true
.
For Example:
let age = 15;
if (age >= 18) {
console.log("You are eligible to Apply!");
}else if (age >= 15) {
console.log("You can attend teen events only.");
}else if(age >= 13){
console.log("Not able to apply right now!");
}else {
console.log("You are still a child!");
}
Output: You can attend teen events only.
// If the age is less then 13 then condition goes towards else
let age = 12;
if (age >= 18) {
console.log("You are eligible to Apply!");
}else if (age >= 15) {
console.log("You can attend teen events only.");
}else if(age >= 13){
console.log("Not able to apply right now!");
}else {
console.log("You are still a child!");
}
Output: You are still a child!
4. switch
Statement
The switch
statement in JavaScript is a control flow statement used for making decisions based on different conditions. It allows you to select and execute a specific block of code based on the value of an expression. in short, It compares a value to a different cases
and runs the code block associated with the matching case. The switch statement is typically used when you have a limited number of possible conditions and want to avoid using nested if-else
statements.
For example:
const grade = 85;
switch (true) {
case grade >= 90:
console.log("A");
break;
case grade >= 80:
console.log("B");
break;
case grade >= 70:
console.log("C");
break;
case grade >= 60:
console.log("D");
break;
default:
console.log("F");
}
Output: B // Because here B matches to the condition grade
There is break
keyword that is used to exit the switch
statement after a matching case is found. If the case doesn't match, the code will continue to execute through the following case
clauses until a break
is encountered or the end of the switch
statement is reached.
5. Conditional Operator (Ternary Operator)
The conditional operator, also known as the ternary operator
, is a powerful tool in JavaScript for writing concise and readable conditional statements. It allows you to evaluate a condition and return one of two expressions based on the result.
For Example:
let age = 15;
let isAge = (age >= 18) ? "You are eligible to apply." : "You are not eligible to apply yet.";
console.log(isAge);
Output: You are not eligible to apply yet.
Let's see how the ternary operator works.
let age = 18;
let isAdult = age >= 18 ? "adult" : "child";
console.log(isAdult);
Output: adult
// Explaination
Here isAdult is a in which we assign this condition.
agr >= 18 ypu are checking the consition here.
? after qustion mark there is a true condition. You can relate it with if.
: after colon there is false condition. You can relate it with else.
Note:
It is a shorthand way to write a simple if-else
statement in one line.