JavaScript If Statement: A Complete Beginner Guide
The if statement is how JavaScript makes decisions. Learn the syntax, how conditions work, and how to avoid the most common beginner mistakes.
An if statement is the simplest way to make a decision in JavaScript. You give it a condition, and it runs a block of code if that condition is true. If the condition is false, it skips the block completely.
if (10 > 5) {
console.log("Ten is larger than five.");
}
// "Ten is larger than five." prints because the condition is trueThat is the core idea. The rest of this article explains the syntax in detail, what counts as a valid condition, and the mistakes beginners make most often.
The if Statement Syntax
Every if statement has three parts:
if (condition) {
// code that runs when condition is true
}- if: the keyword that starts the statement.
- (condition): an expression inside parentheses. JavaScript evaluates it and converts the result to true or false.
- { ... }: the body. Code inside these curly braces runs only when the condition is true.
The condition is always wrapped in parentheses. Forgetting the parentheses is a syntax error.
What Counts as a Condition
The condition can be anything that produces a value: a comparison, a variable, a function call, or even a literal value. JavaScript converts whatever you put there to a boolean.
Comparisons (most common)
const score = 85;
if (score >= 80) {
console.log("Pass");
}
// "Pass" -- 85 >= 80 is trueComparisons return true or false directly, so they are the most natural condition.
Truthy and falsy values
When the condition is not already a boolean, JavaScript converts it. Values that convert to false are called falsy, and values that convert to true are called truthy.
There are exactly six falsy values in JavaScript:
if (false) { } // does not run
if (0) { } // does not run
if ("") { } // does not run (empty string)
if (null) { } // does not run
if (undefined) { } // does not run
if (NaN) { } // does not runEverything else is truthy. This surprises a lot of beginners, because some values that look empty or falsy at a glance actually count as truthy in JavaScript:
if ([]) { console.log("runs"); } // empty array is truthy
if ({}) { console.log("runs"); } // empty object is truthy
if ("false") { console.log("runs"); } // non-empty string is truthy
if ("0") { console.log("runs"); } // non-empty string is truthy
if (-1) { console.log("runs"); } // non-zero number is truthyThe string "false" is truthy because it is a non-empty string. The string "0" is truthy for the same reason. Only the exact six values listed above are falsy.
This automatic conversion lets you write concise checks:
const username = "";
if (username) {
console.log("Hello, " + username);
} else {
console.log("No username provided.");
}
// "No username provided." -- empty string is falsyFor a deeper explanation, see the truthy and falsy values guide.
The if Body
The code inside curly braces is called the body. It can contain any number of statements:
if (isLoggedIn) {
console.log("Loading dashboard...");
fetchUserData();
updateLastLoginTime();
}When the body has exactly one statement, the curly braces are technically optional, and JavaScript will still run that single line correctly without them:
if (isLoggedIn) console.log("Welcome!");This works but is not recommended. If you later add a second statement without adding braces, only the first statement is controlled by the if:
if (isLoggedIn)
console.log("Loading...");
fetchUserData(); // Runs ALWAYS, regardless of isLoggedIn!Always use curly braces. It prevents bugs and makes the code's structure visible at a glance.
The Assignment-vs-Comparison Trap
The most common if statement mistake is using = (assignment) when you meant === (comparison):
// Wrong: = assigns, does not compare
if (role = "admin") {
console.log("Access granted"); // Always runs!
}This code does two things: it assigns "admin" to role, then evaluates "admin" as truthy. The if block always runs, and role is silently overwritten.
// Right: === compares
if (role === "admin") {
console.log("Access granted"); // Only runs when role is "admin"
}Some developers write comparisons with the literal value on the left instead of the variable, specifically to catch this mistake at the syntax level:
if ("admin" === role) { ... } // If you use = here, it is a syntax errorThis is a style choice. The important thing is to pause whenever you see a single equals sign inside an if condition and ask yourself: "Did I mean to assign, or did I mean to compare?"
Combining Multiple Conditions
A single if can check more than one thing using logical operators:
const age = 25;
const hasLicense = true;
if (age >= 18 && hasLicense) {
console.log("You can drive.");
}AND means both conditions must be true. OR means at least one must be true. For more complex combinations, see the logical short-circuiting guide.
Rune AI
Key Insights
- An if statement runs code only when its condition evaluates to true.
- The condition goes inside parentheses: if (condition) { ... }.
- JavaScript automatically converts non-boolean conditions to true or false.
- Six values are falsy: false, 0, '', null, undefined, NaN. Everything else is truthy.
- Always use curly braces, even for single-statement bodies.
- Never use = (assignment) inside a condition when you meant === (comparison).
Frequently Asked Questions
Can I use an if statement without curly braces?
What values count as false inside an if condition?
Can I put an assignment inside an if condition?
Conclusion
The if statement is the most fundamental way to make decisions in JavaScript. It checks a condition inside parentheses, and if that condition is true, runs the code inside curly braces. The condition can be any expression, and JavaScript converts it to a boolean automatically. Remember the six falsy values, always use curly braces, and never confuse = (assignment) with === (comparison) inside the condition.
More in this topic
Is JavaScript Frontend or Backend? Full Guide
JavaScript is both a frontend and backend language. Learn the difference between browser JavaScript and Node.js, what each is used for, and which one you should learn first.
Learn JavaScript Step by Step Tutorial with Real Examples
Follow a hands-on tutorial that teaches JavaScript by building a real interactive page. Write your first variables, functions, and event listeners with examples you can run.
JavaScript Tutorial: Complete Beginner's Guide to Programming in 2025
A structured learning path for absolute beginners. Learn what to study first, how to practice, and which JavaScript concepts matter most when you are starting from zero.