Conditional statements in JavaScript control the flow of program execution by running different code blocks based on whether a specified condition evaluates to true or false.
1. The if Statement
The basic if statement executes a block of code only if the condition inside the parentheses evaluates to a truthy value.
let speed = 75;
if (speed > 60) {
console.log("You are speeding!");
}
2. The if...else Statement
The if...else structure provides an alternative block of code that runs if the initial condition evaluates to a falsy value.
let age = 16;
if (age >= 18) {
console.log("Allowed to vote.");
} else {
console.log("Too young to vote."); // Executes if age is less than 18
}
Real life example
- here the conditional statements are applicable in real life also we also accepts the conditions based on the situations.
Think like this
we get ready to go for our daily classes, the classes start at 9.00 AM.
(if we wakeup at 6.00 AM) then we have lot of time to do our workouts, study, and time to get ready for classes, and have time to eat breakfast.
or (Else if we wakeup at 7.00 AM) then we have time to study, and time to get ready for classes, and have time to eat breakfast.
(Else) the wakeup time later than 7.00 AM, example if we wakeup at 8.00 AM means we don't have enough time to do all our works so we have time to get ready for classes and eat the breakfast. here we need to skip our workouts, and studies because of the situation
we need to go with this condition.
Now we see one real life example with code.
I decided to buy a new phone today.
I have 45000/- in my hand and I visit the mobile shop near me.
I ask the price of the mobile based on the models.
I like iphone so I Ask the price of the base variant. they told the starting price as 70000/- but i have only 45000/- then I need to go for another model because of this condition.
And I also like the pixel phone, so I Ask about it specifications and price then they said the starting price as 55000/- but i have only 45000/- right. because of this condition I need to choose another one.
Then finally I Ask About SAMSUNG mobile phones, like specs and price of some models they said the starting price as 43999/- so I can able to buy that, they suggest me SAMSUNG GALAXY A56 5g as the price point of 43999/- now the condition is satisfied because i have 45000/- in my hand then I go with SAMSUNG.
Example Code
let mybutget=45000;
let iphone_price = 70000;
let pixel_price = 55000;
let samsung_price = 43999;
if(mybutget>=iphone_price){
console.log("I can buy Iphone");
}
else if(mybutget>=pixel_price){
console.log("I can buy Pixel phone");
}
else if(mybutget>=samsung_price){
console.log("I can buy SAMSUNG phone")
}
else{
console.log("I can't able to buy phone")
}
Output
I can buy SAMSUNG phone
=== Code Execution Successful ===
- like this the conditional statement works, first it check the first condition it is true means the condition satisfied and it execute that if() block. are else it checks the another condition as second condition if it true then it execute that if else() block. or it checks another conditions if it have another if else() then it evaluate either the condition satisfied or not, satisfied means that if else() block executes, or it execute the else block it is the final block which execute if not other block conditions satified.
Reference
https://www.geeksforgeeks.org/javascript/conditional-statements-in-javascript/
Top comments (0)