In JavaScript's if-then-else there is technically no elseif branch.

But it works if you write it this way:

if (condition) {

} else if (other_condition) {

} else {

}

To make it obvious what is really happening you can expand the above code using an additional pair of { and }:

if (condition) {

} else {

   if (other_condition) {

   } else {

   }

}

In the first example we're using some implicit JS behavior about {} uses. We can omit these curly braces if there is only one statement inside. Which is the case in this construct, because the inner if-then-else only counts as one statment. The truth is that those are 2 nested if-statements. And not an if-statement with 2 branches, as it may appear on first sight.

This way it resembles the elseif that is present in other languages.

It is a question of style and preference which way you use it.

Answer from Jeff on Stack Overflow
🌐
W3Schools
w3schools.com › js › js_if_else.asp
JavaScript else Statement
JS Examples JS HTML DOM JS HTML ... JS Interview Prep JS Bootcamp JS Certificate ... Use the else statement to specify a block of code to be executed if a condition is false....
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements › if...else
if...else - JavaScript | MDN
May 4, 2026 - 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.
Discussions

if statement - "elseif" syntax in JavaScript - Stack Overflow
With if and else there really is no need of elseif. 2010-10-23T21:12:43.417Z+00:00 ... @Mark, I agree... but it messes me up sometimes because I'm used to languages that have an elseif. I know it's identical, but I wonder what javascript's reason is for leaving it out. More on stackoverflow.com
🌐 stackoverflow.com
conditional operator - Javascript one line If...else...else if statement - Stack Overflow
I know you can set variables with one line if/else statements by doing var variable = (condition) ? (true block) : (else block), but I was wondering if there was a way to put an else if statement in More on stackoverflow.com
🌐 stackoverflow.com
If else chains Basic JS
I am confused about how to go about doing this. Basic JavaScript: Chaining If else statements More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
13
0
October 14, 2019
From nothing to important actions: agents that act morally — EA Forum
You may start reading here, or jump to the “Comment” section or to the “Takeaways”. If none of these starting points seem interesting to you, the ent… More on forum.effectivealtruism.org
🌐 forum.effectivealtruism.org
April 27, 2026
🌐
Medium
geraldclarkaudio.medium.com › javascript-if-else-else-if-statements-26d18456f304
JavaScript — If, Else, & Else If Statements | by Gerald Clark | Medium
May 23, 2024 - If, else, and else if statements do the same thing except they don’t loop. Heres some examples: I have age set to 20 and below I’m checking whether it is greater than, less than, or equal to 20. Since age is equal to 20, the first two checks do not run. Only the else block runs. You can also check things like the length of an array: ... Since there are 4 strings in the pokemon array, that if statement will run becvause it is checking whether pokemon.length is greater than 3, and it is!
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-if-else-else-if
JavaScript if, else and else if - GeeksforGeeks
April 15, 2025 - In this example, if the value of ... printed to the console. The else statement follows an if statement and provides an alternative block of code to run when the condition in the if statement is false....
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-if-else
JavaScript if-else - GeeksforGeeks
July 28, 2025 - // JavaScript program to illustrate ... console.log("i is greater than 15"); } } ... The if-else-if ladder is used to check multiple conditions in sequence....
🌐
Mimo
mimo.org › glossary › javascript › else-if
JavaScript else if statement: handle multiple conditions
Here, the first condition checks if hour is less than 12. If it's not, JavaScript moves to the else block, where it evaluates the specified condition in else if. If that condition is also false, it moves to the else statement.
🌐
Programiz
programiz.com › javascript › if-else
JavaScript if...else Statement (with Examples)
These conditional tasks can be achieved using the if...else statement. We use the if keyword to execute code based on some specific condition. ... The if keyword checks the condition inside the parentheses ().
🌐
DEV Community
dev.to › makstyle119 › understanding-if-else-and-else-if-statements-in-javascript-12im
Understanding if, else, and else if statements in JavaScript - DEV Community
April 6, 2023 - the if-else statement in JavaScript is an essential tool for controlling the flow of code based on specific conditions. It evaluates a condition and executes a code block if it is true, otherwise it skips to the next condition or the else statement.
🌐
Quora
quora.com › What-is-the-recommended-way-to-write-if-statements-in-JavaScript-Is-it-better-to-use-if-or-if-else-Why
What is the recommended way to write if statements in JavaScript? Is it better to use 'if' or 'if else'? Why? - Quora
Answer (1 of 3): JavaScript has boolean if statements. Some languages like FORTRAN gives you three options ( ). With only two choices you have to make tertiary logic using boolean tests and one easy way to do that is with else if. if ( a
🌐
javascript.com
javascript.com › learn › conditionals
JavaScript Conditionals: The Basics with Examples | JavaScript.com
There are multiple different types ... for a block of code. “Else” statements: where if the same condition is false it specifies the execution for a block of code....
🌐
W3Schools
w3schools.com › java › java_conditions.asp
Java If ... Else
In the next chapters, you will also learn how to handle else (when the condition is false), else if (to test multiple conditions), and switch (to handle many possible values). ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
Top answer
1 of 11
272

Sure, you can do nested ternary operators but they are hard to read.

var variable = (condition) ? (true block) : ((condition2) ? (true block2) : (else block2))
2 of 11
69

tl;dr

Yes, you can... If a then a, else if b then if c then c(b), else b, else null

a ? a : (b ? (c ? c(b) : b) : null)

a
  ? a
  : b
      ? c
        ? c(b)
        : b
      : null

longer version

Ternary operator ?: used as inline if-else is right associative. In short this means that the rightmost ? gets fed first and it takes exactly one closest operand on the left and two, with a :, on the right.

Practically speaking, consider the following statement (same as above):

a ? a : b ? c ? c(b) : b : null

The rightmost ? gets fed first, so find it and its surrounding three arguments and consecutively expand to the left to another ?.

   a ? a : b ? c ? c(b) : b : null
                 ^                  <---- RTL
1.            |1-?-2----:-3|
             ^ <-
2.        |1-?|--2---------|:-3---|
     ^ <-
3.|1-?-2-:|--3--------------------|

result: a ? a : (b ? (c ? c(b) : b) : null)

This is how computers read it:

  1. Term a is read.
    Node: a
  2. Nonterminal ? is read.
    Node: a ?
  3. Term a is read.
    Node: a ? a
  4. Nonterminal : is read.
    Node: a ? a :
  5. Term b is read.
    Node: a ? a : b
  6. Nonterminal ? is read, triggering the right-associativity rule. Associativity decides:
    node: a ? a : (b ?
  7. Term c is read.
    Node: a ? a : (b ? c
  8. Nonterminal ? is read, re-applying the right-associativity rule.
    Node: a ? a : (b ? (c ?
  9. Term c(b) is read.
    Node: a ? a : (b ? (c ? c(b)
  10. Nonterminal : is read.
    Node: a ? a : (b ? (c ? c(b) :
  11. Term b is read.
    Node: a ? a : (b ? (c ? c(b) : b
  12. Nonterminal : is read. The ternary operator ?: from previous scope is satisfied and the scope is closed.
    Node: a ? a : (b ? (c ? c(b) : b) :
  13. Term null is read.
    Node: a ? a : (b ? (c ? c(b) : b) : null
  14. No tokens to read. Close remaining open parenthesis.
    #Result is: a ? a : (b ? (c ? c(b) : b) : null)

Better readability

The ugly oneliner from above could (and should) be rewritten for readability as:
(Note that the indentation does not implicitly define correct closures as brackets () do.)

a
  ? a
  : b
      ? c
        ? c(b)
        : b
      : null

for example

return a + some_lengthy_variable_name > another_variable
        ? "yep"
        : "nop"

More reading

Mozilla: JavaScript Conditional Operator
Wiki: Operator Associativity


Bonus: Logical operators

var a = 0 // 1
var b = 20
var c = null // x=> {console.log('b is', x); return true} // return true here!

a
  && a
  || b
      && c
        && c(b) // if this returns false, || b is processed
        || b
      || null

Using logical operators as in this example is ugly and wrong, but this is where they shine...

"Null coalescence"

This approach comes with subtle limitations as explained in the link below. For proper solution, see Nullish coalescing in Bonus2.

function f(mayBeNullOrFalsy) {
  var cantBeNull = mayBeNullOrFalsy || 42                    // "default" value
  var alsoCantBe = mayBeNullOrFalsy ? mayBeNullOrFalsy : 42  // ugly...
  ..
}

Short-circuit evaluation

false && (anything) // is short-circuit evaluated to false.
true || (anything)  // is short-circuit evaluated to true.

Logical operators
Null coalescence
Short-circuit evaluation


Bonus2: new in JS

Proper "Nullish coalescing"

developer.mozilla.org~Nullish_coalescing_operator

function f(mayBeNullOrUndefined, another) {
  var cantBeNullOrUndefined = mayBeNullOrUndefined ?? 42
  another ??= 37 // nullish coalescing self-assignment
  another = another ?? 37 // same effect
  ..
}

Optional chaining

Stage 4 finished proposal https://github.com/tc39/proposal-optional-chaining https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining

// before
var street = user.address && user.address.street
// after
var street = user.address?.street

// combined with Nullish coalescing
// before
var street = user.address
  ? user.address.street
  : "N/A"

// after
var street = user.address?.street ?? "N/A"

// arrays
obj.someArray?.[index]

// functions
obj.someMethod?.(args)
🌐
SheCodes
shecodes.io › athena › 9125-a-guide-to-using-the-if-else-statement-in-javascript
[JavaScript] - A Guide to Using the If-Else Statement in | SheCodes
Understand the if-else statement in JavaScript and learn how to use it to execute specific code based on a condition.
🌐
Algor Education
cards.algoreducation.com › en › content › S_6DXDE3 › javascript-if-else-conditional-structure
The Importance of If-Else Statements in JavaScript | Algor Cards
If-Else statements direct program flow by executing code only when certain conditions are met. ... Essential for logical decisions in code, impacts efficiency and is foundational for complex programming concepts. ... The JavaScript If-Else statement is an essential control structure in the JavaScript language, pivotal for decision-making in web development.
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript
JavaScript | MDN
May 22, 2026 - Learn how do-while, for-in, for-of, try-catch, let, var, const, if-else, switch, and more JavaScript statements and keywords work.
🌐
freeCodeCamp
forum.freecodecamp.org › curriculum help
If else chains Basic JS - Curriculum Help - The freeCodeCamp Forum
October 14, 2019 - I am confused about how to go about doing this. Basic JavaScript: Chaining If else statements
🌐
United
united.com › en › us › newsroom › announcements › cision-125457
released a press statement
United Airlines - Airline Tickets, Travel Deals and Flights If you're seeing this message, that means JavaScript has been disabled on your browser, please enable JS to make this app work
🌐
Effective Altruism Forum
forum.effectivealtruism.org › posts › pSZm8LzxuFKSR84Sk › from-nothing-to-important-actions-agents-that-act-morally
From nothing to important actions: agents that act morally — EA Forum
April 27, 2026 - It seems uncontestable that some visual experiences look darker than others[1]. It is one way in which visual experiences look different from each other. Another difference is colour: some experiences look green more than they look purple. Let’s try to attack the above statement. How could it not be the case that some experiences look darker than others? If you somehow couldn’t perceive differences in scales of grey, then maybe you wouldn’t say that some visual experiences look darker than others.