Conditional Statements in JavaScript
Making Decisions in Code
- In JavaScript, conditional statements are used to make decisions in a program.
- They help the program choose what action to perform based on whether a condition is true or false.
- Conditional statements make your code smart and interactive.
For example:
- If the user is 18 or older β allow voting
- Else β show not eligible message
JavaScript mainly provides these conditional statements:
ifif...elseif...else if...elseswitch
Letβs understand each one with examples.
1) if Statement
The if statement is used when you want to execute a block of code only if a condition is true.
Syntax
if (condition) {
// code to execute
}
Example
let age = 20;
if (age >= 18) {
console.log("Eligible to vote");
}
Output
Eligible to vote
Here, since age >= 18 is true, the message gets printed.
2) else Statement
The else block runs when the condition inside if is false.
Syntax
if (condition) {
// true block
} else {
// false block
}
Example
let age = 16;
if (age >= 18) {
console.log("Eligible to vote");
} else {
console.log("Not eligible to vote");
}
Output
Not eligible to vote
This is useful when there are two possible outcomes.
3) else if Statement
This is used when you need to check multiple conditions.
Syntax
if (condition1) {
// block 1
} else if (condition2) {
// block 2
} else {
// default block
}
Example
let marks = 85;
if (marks >= 90) {
console.log("Grade A+");
} else if (marks >= 75) {
console.log("Grade A");
} else if (marks >= 50) {
console.log("Pass");
} else {
console.log("Fail");
}
Output
Grade A
The program checks conditions from top to bottom and runs the first true condition.
4) switch Statement
The switch statement is used when you want to compare one variable with multiple values.
It is often cleaner than using many else if conditions.
Syntax
switch (expression) {
case value1:
// code
break;
case value2:
// code
break;
default:
// default code
}
Example
let day = 3;
switch (day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
case 3:
console.log("Wednesday");
break;
default:
console.log("Invalid day");
}
Output
Wednesday
Why break is Important?
The break statement stops the switch after a matching case is found.
Without break, the code continues executing the next cases.
Example Without Break
let day = 1;
switch (day) {
case 1:
console.log("Monday");
case 2:
console.log("Tuesday");
}
Output
Monday
Tuesday
This happens because there is no break.
Real-Life Example
1.
let temperature = 35;
if (temperature > 30) {
console.log("It's hot outside");
} else {
console.log("Weather is cool");
}
- Conditional statements help programs make decisions just like humans do.
2.
let team = "CSK";
if (team === "MI") {
console.log("Mumbai Indians");
}
else if (team === "CSK") {
console.log("Chennai Super Kings");
}
else if (team === "RCB") {
console.log("Royal Challengers Bengaluru");
}
else {
console.log("Invalid Team");
}
Conclusion
Conditional statements are one of the most important concepts in JavaScript.
They help in:
- decision making
- validating inputs
- checking conditions
- controlling program flow
Top comments (0)