Meaning
Conditional statements allow you to execute specific blocks of code based on conditions. If the condition meets then a particular block of action will be executed otherwise it will execute another block of action that satisfies that particular condition.
Simple If
It executes a block of code if a condition is satisfied and will do nothing if the same results in false.
let a = 20
if (a < 100){
a += 20
}
/* here, if the value of A is less than 100 only then it will enter in
the block otherwise will do nothing */
if....else
It executes if
block if the condition is true and executes else
if the condition results in false. Only will execute for a condition.
Use
if-else
when you need a default action if the condition isfalse
.An
if-else
statement includes anelse
block that runs if theif
condition isfalse
let a = 100
if (a < 20){
console.log("Smaller")
}
else{
console.log("bigger") // result
}
else.....if.....else ladder
The if-else ladder evaluates conditions from top to bottom. Once a condition is true, the corresponding block of code executes, and no further conditions are checked.
The
else if
statement allows for multiple conditions, providing a clear structure to handle complex decision-making.The syntax is simple just add another
ifelse
afterelse
of previous flow
let a = 20
if (a < 10){
console.log("10");
} else if (a < 20){
console.log("20");
} else if (a < 30){
console.log("30"); // result
}
Nested ifelse
It is used to check more complex conditions
we add another
if
insideif
condition.This approach allows you to create multiple layers of conditions, where the execution path depends on a combination of different criteria.
Every
if
must have its correspondingelse
let a = 120
if (a < 100){
if (a > 200){
console.log(a)
} else{
console.log("no") // result
}
} else {
console.log("exit")
}
/* It will first check whether A is smaller than 100 or not. If yes do
check if the same is greater than 200 or not. If yes, do print the value
of A. If not, print "no" but if the first condition return false than do
print "exit" */
Switch case
Use
switch
when you have a single expression with multiple possible outcomes.Ensure
break
is used to prevent unintended fallthrough unless it's intentional.Keep the
default
case to handle unexpected values or provide a fallback.Avoid using
switch
for complex conditions or ranges;if-else
or other control structures are better suited for such cases.the
switch
statement provides an alternative to multipleif-else
conditions when you need to evaluate a single expression against several possible outcomes.A basic
switch
statement uses theswitch
keyword, followed by an expression to evaluate, and then one or morecase
statements to handle specific outcomes. Thedefault
keyword defines a block to execute if none of the cases match.Break
Eachcase
typically ends withbreak
to exit theswitch
block. Withoutbreak
, execution "falls through" to the next case, which might be intentional in some scenarios.default
Thedefault
case is like anelse
block, handling any situation not covered by the definedcases
let day = "Monday";
switch (day) {
case "Monday":
console.log("Start of the workweek.");
break;
case "Wednesday":
console.log("Midweek.");
break;
case "Friday":
console.log("End of the workweek.");
break;
case "Saturday":
case "Sunday":
console.log("Weekend.");
break;
default:
console.log("Unknown day.");
}
/* if day == "Monday" print first statement and break the flow
of checking conditions and the same will check if first found
no match. In case "saturday" and "sunday" if result in any of them
then print "weekend" means they share same result */
You have Learned JavaScript conditional statements.................
If you want more such content then make sure to following me on all available platform
Thank you for reading ๐ค
Written By Mohit Soni