JavaScript if Statement
Table of Contents + −
In the previous lesson, we learned how to safely read nested values with optional chaining. Now let’s start controlling the flow of our programs, beginning with the if statement that runs code only when a condition is true.
🚦 What is an if Statement?
An if statement runs a block of code only when a condition is true. If the condition is false, JavaScript skips the block and moves on. This is how our programs make decisions.
if (condition) { // this code runs only when condition is true}This is the basic shape of every if statement:
- The keyword
ifstarts the statement. conditiongoes inside the parentheses( )and must evaluate totrueorfalse.- The code to run goes inside the curly braces
{ }, and JavaScript runs it only when the condition istrue.
🧠 How the Condition Works
The condition is any expression that evaluates to true or false. We usually build it with the comparison operators we learned earlier, such as >=, ===, and <.
| Condition | Meaning | Result |
age >= 18 | Is age at least 18? | true or false |
name === "Alex" | Is name exactly “Alex”? | true or false |
total < 50 | Is total below 50? | true or false |
👤 A Simple Example
Let’s check whether a person is old enough to vote.
const age = 20;
if (age >= 18) { console.log("You can vote!");}Let’s walk through what each line does:
- The first line stores
20in a variable namedage. - The
ifline asksage >= 18, which istruebecause20is at least18. - Since the condition is
true, JavaScript runs the block and printsYou can vote!.
If we set age to 15, the condition is false, the block is skipped, and nothing prints.
🛒 A Practical Example
Let’s use an if statement to give free shipping when a cart total is high enough.
const cartTotal = 75;
if (cartTotal >= 50) { console.log("You qualify for free shipping!");}The store sets a rule: orders of 50 or more ship for free. Here’s how the code applies that rule:
- The first line stores the order amount,
75, incartTotal. - The
ifline checkscartTotal >= 50, which istruebecause75is more than50. - The block runs, so the customer sees the free shipping message.
This is the same pattern we use for discounts, access checks, and form validation.
✨ Truthy and Falsy Conditions
The condition does not have to be a comparison. JavaScript checks whether the value is “truthy” or “falsy”, which means whether it acts like true or false. We covered this idea with the logical operators.
const username = "Sam";
if (username) { console.log("Welcome, " + username);}Here’s what happens line by line:
- The first line stores the string
"Sam"inusername. - The
ifline usesusernamedirectly as the condition, with no comparison operator. - A non-empty string is truthy, so the block runs and prints
Welcome, Sam.
Values like 0, "", null, and undefined are falsy, so they would skip the block.
Falsy values to remember
false, 0, "" (empty string), null, undefined, and NaN are all
falsy. Everything else is truthy.
⚠️ Common Mistakes to Avoid
| Mistake | Problem | Solution |
Using = instead of === | if (age = 18) assigns instead of comparing | Use === to compare: if (age === 18) |
| Forgetting the curly braces | Only the next line is tied to the if | Always wrap the block in { } |
Semicolon right after if (...) | if (x > 0); ends the statement early | Never put ; before the { } block |
Compare, don't assign
A single = assigns a value, while === compares values. Using = inside an
if condition is a classic bug that makes the condition almost always run.
🔧 Try It Yourself!
- Create an
agevariable and use anifstatement to print a message whenage >= 18. - Create a
cartTotalvariable and print a free shipping message when it is50or more. - Store a name in a variable and use a truthy check to greet the user only when the name is not empty.
- Change your numbers so the conditions become false, then run the code and confirm nothing prints.
🧩 What You’ve Learned
- ✅ An
ifstatement runs a block only when its condition istrue - ✅ The syntax is
if (condition) { ... } - ✅ Conditions usually use comparison operators like
>=and=== - ✅ Conditions can also rely on truthy and falsy values
- ✅ Watch out for
=vs===, missing braces, and a stray semicolon afterif (...)
🚀 What’s Next?
Now that we can run code when a condition is true, let’s learn how to handle the false case too. Let’s continue to if else.