Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The if...else statement executes a statement if a specified condition is truthy. If the condition is falsy, another statement in the optional else clause will be executed.
An expression that is considered to be either truthy or falsy.
statement1Statement that is executed if condition is truthy. Can be any statement, including further nested if statements. To execute multiple statements, use a block statement ({ /* ... */ }) to group those statements. To execute no statements, use an empty statement.
statement2Statement that is executed if condition is falsy and the else clause exists. Can be any statement, including block statements and further nested if statements.
Multiple if...else statements can be nested to create an else if clause. Note that there is no elseif (in one word) keyword in JavaScript.
To see how this works, this is how it would look if the nesting were properly indented:
The conditions are evaluated in order until one evaluates to true. At that point, the associated statement is executed and the rest of the else if clauses are skipped.
To execute multiple statements within a clause, use a block statement ({ /* ... */ }) to group those statements.
Not using blocks may lead to confusing behavior, especially if the code is hand-formatted. For example:
This code looks innocent — however, executing checkValue(1, 3) will log "a is not 1". This is because in the case of dangling else, the else clause will be connected to the closest if clause. Therefore, the code above, with proper indentation, would look like:
In general, it is a good practice to always use block statements, especially in code involving nested if statements.
Do not confuse the primitive Boolean values true and false with truthiness or falsiness of the Boolean object. Any value that is not false, undefined, null, 0, -0, NaN, or the empty string (""), and any object, including a Boolean object whose value is false, is considered truthy when used as the condition. For example:
Note that there is no elseif syntax in JavaScript. However, you can write it with a space between else and if:
You should almost never have an if...else with an assignment like x = y as a condition:
Because unlike while loops, the condition is only evaluated once, so the assignment is only performed once. The code above is equivalent to:
Which is much clearer. However, in the rare case you find yourself wanting to do something like that, the while documentation has a Using an assignment as a condition section with our recommendations.
| ECMAScript® 2027 Language Specification # sec-if-statement |
Enable JavaScript to view this browser compatibility table.
This page was last modified on May 4, 2026 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.