When you start learning JavaScript, one of the first things you need to understand is decision making. In real life, we make decisions all the time. For example:
- If it is raining, you take an umbrella.
- If you are hungry, you eat food.
Similarly, in programming, we use the JavaScript if statement to make decisions.
So, let’s understand it step by step in a very simple way.
What is a JavaScript If Statement?
The JavaScript if statement allows your program to run a block of code only when a condition is true.
In simple words: 👉 “If something is true, then do this.”
Basic Syntax of an If Statement
Here is the basic syntax of an if statement in JavaScript –
if (condition) {
// code to run if condition is true
}
Example:
let age = 18;
if (age >= 18) {
console.log("You can vote");
}
Explanation:
- We created a variable age.
- Then we checked: is age greater than or equal to 18?.
- Since the condition is true, the message gets printed.
👉 Output: You can vote.
How It Works (Simple Logic)
Let’s break it down like a real-life situation:
👉 Condition: age >= 18
👉 If true → Allow voting
👉 If false → Do nothing
So basically, the code runs only when the condition becomes true.
If Statement with Else
Now, what if the condition is false? For that, we use else with an if statement.
Syntax
if (condition) {
// runs if true
} else {
// runs if false
}
Example:
let age = 16;
if (age >= 18) {
console.log("You can vote");
} else {
console.log("You cannot vote");
}
Explanation:
- Condition is false (16 < 18)
- So the else block runs
👉 Output: You cannot vote.
If…Else If…Else Statement
Sometimes, we have multiple conditions. For example, checking grades.
Syntax
if (condition1) {
// code
} else if (condition2) {
// code
} else {
// default code
}
Example:
let marks = 75;
if (marks >= 90) {
console.log("Grade A");
} else if (marks >= 60) {
console.log("Grade B");
} else {
console.log("Grade C");
}
Explanation:
- Marks = 75
- First condition fails
- Second condition is true
- So it prints Grade B
👉 Output: Grade B
Nested If Statement
You can also use an if statement inside another if statement.
Example:
let age = 20;
let hasID = true;
if (age >= 18) {
if (hasID) {
console.log("Entry allowed");
}
}
Explanation:
- First condition is true (age >= 18)
- The second condition is also true (hasID)
- So the message is printed
👉 Output: Entry allowed
Real-Life Example (Very Important)
Let’s take a practical example:
let isLoggedIn = true;
if (isLoggedIn) {
console.log("Welcome back!");
} else {
console.log("Please log in");
}
👉 This is exactly how websites decide:
- Show dashboard → if user is logged in
- Show login page → if not
Common Mistakes Beginners Make
1. Using = instead of ==
if (x = 10) // ❌ Wrong
👉 This assigns value instead of comparing.
Correct:
if (x == 10) // ✅
2. Forgetting Curly Braces
if (age >= 18)
console.log("Allowed"); // risky
👉 Always use {} for better readability
3. Writing Complex Conditions
Beginners often write confusing conditions. 👉 Keep it simple and readable.
When Should We Use an If Statement?
Use JavaScript if statement when:
- You want to check a condition.
- You want to control program flow.
- You want to make decisions in your code.
Quick Summary
- The JavaScript if statement is used for decision-making
- It runs code only when a condition is true
- You can use:
- if
- if…else
- if…else if…else
- It is one of the most important concepts in programming
FAQs
1. What is an if statement in JavaScript?
It is used to execute code only when a condition is true.
2. Can we use multiple conditions in if?
Yes, you can use else if to check multiple conditions.
3. What happens if the condition is false?
The code inside the if block will not run. If else is present, it will run instead.
4. Is the if statement important for beginners?
Yes, it is one of the most basic and important concepts in JavaScript.
Read Also – Switch Case in JavaScript

