JavaScript If Else Statements Explained for Beginners
Learn JavaScript if, else, and else if statements with simple, line-by-line beginner examples covering comparisons, && / ||, and the ternary operator.
Learn JavaScript if, else, and else if statements with simple, line-by-line beginner examples covering comparisons, && / ||, and the ternary operator.
If you've searched for "javascript if else for beginners," here's the short answer: an if/else statement lets your code make a decision. It checks whether something is true, and runs one block of code if it is, or a different block if it isn't. That's it. In this guide you'll learn exactly how to write if, else, and else if statements in JavaScript, with small, copy-pasteable examples explained line by line.
A condition is a question that JavaScript can answer with true or false. You write conditions using comparison operators, like checking if one number is bigger than another, or if two values are equal. JavaScript looks at the condition, decides if it's true or false, and then runs the matching block of code.
let age = 20;
console.log(age > 18);
console.log(age === 20);
console.log(age === 21);Line 1 creates a variable called age and sets it to 20. Line 3 asks "is age greater than 18?" and prints true, because 20 is bigger than 18. Line 4 asks "is age exactly equal to 20?" and prints true. Line 5 asks "is age exactly equal to 21?" and prints false, since age is 20, not 21. Each of these lines produces a true or false value, and that value is what an if statement checks.
An if statement runs a block of code only when its condition is true. If the condition is false, JavaScript simply skips that block and moves on.
let age = 20;
if (age >= 18) {
console.log("You can vote.");
}Line 3 starts the if statement. The condition, age >= 18, is written inside the parentheses. Because age is 20, this condition is true, so the code between the curly braces { } on line 4 runs, printing "You can vote." If age were 15 instead, the condition would be false and nothing would print at all, because there's no else to catch that case yet.
Most of the time you want something to happen even when the condition is false. That's what else is for. It runs a block of code only when the if condition did not match.
let age = 15;
if (age >= 18) {
console.log("You can vote.");
} else {
console.log("You are too young to vote.");
}Line 3 checks the same condition as before, but now age is 15, so age >= 18 is false. JavaScript skips the if block and jumps straight to the else block on line 5, which prints "You are too young to vote." Notice that only one of the two messages ever prints, never both.
Sometimes two outcomes aren't enough. When you need to check several conditions in order, add one or more else if blocks between if and else. JavaScript checks each condition from top to bottom and runs the first one that's true, then stops.
let score = 72;
if (score >= 90) {
console.log("Grade: A");
} else if (score >= 80) {
console.log("Grade: B");
} else if (score >= 70) {
console.log("Grade: C");
} else {
console.log("Grade: F");
}JavaScript checks line 3 first: is score >= 90? No, 72 is not. It moves to line 5: is score >= 80? No. It moves to line 7: is score >= 70? Yes, 72 is at least 70, so it prints "Grade: C" and stops checking the rest, skipping line 9 entirely. Order matters here: if you had written score >= 70 first, every score of 70 or higher would stop there and never reach the A or B checks.
These are the building blocks you'll use inside almost every condition you write.
| Operator | Meaning | Example |
|---|---|---|
| === | Equal to (value and type) | age === 18 |
| !== | Not equal to | age !== 18 |
| > | Greater than | age > 18 |
| < | Less than | age < 18 |
| >= | Greater than or equal to | age >= 18 |
| <= | Less than or equal to | age <= 18 |
| && | AND — both sides must be true | age >= 18 && hasId |
| || | OR — at least one side must be true | isAdmin || isOwner |
| ! | NOT — flips true to false | !isLoggedIn |
Use === instead of == in JavaScript. The triple-equals checks that both the value and the type match, which avoids confusing bugs where, for example, the text "20" and the number 20 would otherwise be treated as equal.
Real decisions often depend on more than one thing. You can combine conditions using && (AND) and || (OR) instead of writing nested if statements.
let age = 25;
let hasTicket = true;
if (age >= 18 && hasTicket) {
console.log("Welcome, you can enter.");
} else {
console.log("Entry denied.");
}Line 4 uses && between two conditions: age >= 18 and hasTicket. With &&, both conditions must be true for the whole thing to be true. Here age is 25 (true) and hasTicket is true, so the combined condition is true and "Welcome, you can enter." prints. If hasTicket were false, the combined condition would be false even though the age check passed, and the else block would run instead.
When your if/else only sets a value or picks between two short results, the ternary operator (? and :) lets you write it in one line. It's optional, but you'll see it often in other people's code.
let age = 20;
let message = age >= 18 ? "Adult" : "Minor";
console.log(message);Line 3 reads as: "if age >= 18 is true, use the value before the colon (\"Adult\"); otherwise, use the value after the colon (\"Minor\")." The result is stored in the variable message. This line does exactly the same job as a four-line if/else block, just written more compactly.
Here's how if/else looks in a task you'll actually write: checking a username and password before letting someone log in.
let username = "alex";
let password = "hunter2";
if (username === "" || password === "") {
console.log("Please fill in both fields.");
} else if (password.length < 8) {
console.log("Password must be at least 8 characters.");
} else {
console.log("Login successful!");
}Line 4 checks if either field is empty using ||, so a missing username OR a missing password both trigger the same warning. If both fields have something in them, line 6 checks password.length, which counts the characters in the password string; here "hunter2" has 7 characters, so this condition is true and the warning about the 8-character minimum prints. Only if neither earlier condition matched would the code reach the final else and print "Login successful!".
Once you're comfortable making decisions with if/else, the next step is repeating actions automatically with loops.
Learn JavaScript loops nextif/else checks conditions like comparisons and ranges (age >= 18), while switch compares one value against a list of exact matches. For two or three simple true/false checks, if/else is usually clearer; for many exact-value checks on the same variable, switch can be tidier.
Yes. An if statement works fine on its own. If the condition is false and there's no else, JavaScript just skips the block and continues with the rest of the program — nothing runs and nothing breaks.
== compares values after converting types if needed, which can produce surprising results, like "20" == 20 being true. === compares both the value and the type, so "20" === 20 is false. Using === keeps your conditions predictable.
JavaScript treats false, 0, "" (empty string), null, undefined, and NaN as falsy, meaning they act like false inside a condition. Every other value, including any non-empty string or non-zero number, is treated as truthy.
There's no fixed limit, but if you find yourself writing more than four or five else if blocks in a row, it's usually a sign to use a switch statement or an object lookup instead, since long chains get harder to read and maintain.
Want help building the rest of your web app, not just the JavaScript basics?
Talk to FepiqOccasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.
Keep reading
Learn SQL CASE WHEN step by step. This beginner guide shows how to label, group and sort data with simple examples you can copy and run today.
PostgreSQL ILIKE explained for beginners: search text without worrying about case, learn % and _ wildcards, and copy simple examples you can run today.
Let's build something
Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.