JavaScript If Conditions
JavaScript uses conditional statements to perform different actions based on different conditions.
Note:
Conditional statements help programs make decisions.
JavaScript provides the following conditional statements:
- if Statement
- if...else Statement
- else if Statement
- Nested if Statement
These statements control the flow of program execution.
The if statement executes a block of code only if the specified condition is true.
Syntax:
if(condition){
statements;
}
Example:
if(1>0){
document.write("1 is greater than 0");
}
Output:
1 is greater than 0
Since the condition is true, the statement executes.
Example:
if(1<0){
document.write("1 is less than 0");
}
The condition is false, so nothing is displayed.
Variables can also be used in conditions.
Example:
var age=20;
if(age>=18){
document.write("Eligible to vote");
}
Output:
Eligible to vote
Comparison operators are commonly used with if statements.
| Operator |
Meaning |
| == |
Equal to |
| === |
Strict equal to |
| != |
Not equal to |
| > |
Greater than |
| < |
Less than |
| >= |
Greater than or equal to |
| <= |
Less than or equal to |
The else statement executes when the if condition is false.
Syntax:
if(condition){
statements;
}
else{
statements;
}
Example:
var mySal=4000;
var yourSal=16000;
if(mySal>yourSal){
document.write("My salary is greater");
}
else{
document.write("My salary is less than or equal");
}
Output:
My salary is less than or equal.
Use else if when multiple conditions need to be checked.
Syntax:
if(condition1){
...
}
else if(condition2){
...
}
else{
...
}
var marks=75;
if(marks>=80){
document.write("Grade A");
}
else if(marks>=60){
document.write("Grade B");
}
else{
document.write("Grade C");
}
Output:
Grade B
JavaScript allows multiple else if conditions.
Example:
var num=0;
if(num>0){
document.write("Positive");
}
else if(num<0){
document.write("Negative");
}
else if(num==0){
document.write("Zero");
}
Output:
Zero
An if statement inside another if statement is called a nested if.
Example:
var age=20;
if(age>=18){
if(age>=21){
document.write("Adult");
}
else{
document.write("Young Adult");
}
}
Output:
Young Adult
A school can use if conditions to check whether a student has passed.
Example:
var marks=45;
if(marks>=33){
document.write("Pass");
}
else{
document.write("Fail");
}
Output:
Pass
- Helps in decision making.
- Controls program flow.
- Executes code based on conditions.
- Makes programs smarter.
- Supports multiple conditions.
- Easy to understand and implement.
- Use if to execute code when a condition is true.
- Use else when the condition is false.
- Use else if for multiple conditions.
- Only one else block is allowed.
- Multiple else if blocks are allowed.
- Nested if statements are supported.
- Comparison operators are commonly used with conditions.
- Conditional statements control program flow.
- JavaScript supports if, else, and else if.
- Conditions evaluate to true or false.
- Nested conditions are possible.
- Decision making makes programs interactive.
- Conditions can use variables and expressions.
| Statement |
Purpose |
| if |
Execute when condition is true |
| else |
Execute when condition is false |
| else if |
Check additional conditions |
| Nested if |
if inside another if |
- Keep conditions simple.
- Use meaningful variable names.
- Avoid deeply nested conditions.
- Use else if instead of multiple separate if statements.
- Use === for strict comparison when possible.
- Indent code properly for readability.
🧠 Quick Quiz