Lesson 19 of 30 – JavaScript If Conditions
53%

JavaScript If Conditions

JavaScript uses conditional statements to perform different actions based on different conditions.

Note: Conditional statements help programs make decisions.
What are If Conditions?

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

The if statement executes a block of code only if the specified condition is true.

Syntax:
if(condition){
statements;
}
Simple if Example
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.

False Condition
Example:
if(1<0){
document.write("1 is less than 0");
}

The condition is false, so nothing is displayed.

Using Variables in if

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

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

The else statement executes when the if condition is false.

Syntax:
if(condition){
statements;
}
else{
statements;
}
Example of else Statement
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.

The else if Statement

Use else if when multiple conditions need to be checked.

Syntax:
if(condition1){
...
}
else if(condition2){
...
}
else{
...
}
Example of else if
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

Multiple else if Statements

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

Nested if Statement

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

Real-Life Example

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

Advantages of if Conditions
  • Helps in decision making.
  • Controls program flow.
  • Executes code based on conditions.
  • Makes programs smarter.
  • Supports multiple conditions.
  • Easy to understand and implement.
Points to Remember
  • 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.
Key Points
  • 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.
Quick Revision
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
Best Practices
  • 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

Which statement executes when the if condition is false?