First of all, a ternary expression is not a replacement for an if/else construct - it's an equivalent to an if/else construct that returns a value. That is, an if/else clause is code, a ternary expression is an expression, meaning that it returns a value.

This means several things:

  • use ternary expressions only when you have a variable on the left side of the = that is to be assigned the return value
  • only use ternary expressions when the returned value is to be one of two values (or use nested expressions if that is fitting)
  • each part of the expression (after ? and after : ) should return a value without side effects (the expression x = true returns true as all expressions return the last value, but it also changes x without x having any effect on the returned value)

In short - the 'correct' use of a ternary expression is

var resultofexpression = conditionasboolean ? truepart: falsepart;

Instead of your example condition ? x=true : null ;, where you use a ternary expression to set the value of x, you can use this:

 condition && (x = true);

This is still an expression and might therefore not pass validation, so an even better approach would be

 void(condition && x = true);

The last one will pass validation.

But then again, if the expected value is a boolean, just use the result of the condition expression itself

var x = (condition); // var x = (foo == "bar");

UPDATE

In relation to your sample, this is probably more appropriate:

defaults.slideshowWidth = defaults.slideshowWidth || obj.find('img').width()+'px';
Answer from Sean Kinsey on Stack Overflow
Top answer
1 of 13
363

First of all, a ternary expression is not a replacement for an if/else construct - it's an equivalent to an if/else construct that returns a value. That is, an if/else clause is code, a ternary expression is an expression, meaning that it returns a value.

This means several things:

  • use ternary expressions only when you have a variable on the left side of the = that is to be assigned the return value
  • only use ternary expressions when the returned value is to be one of two values (or use nested expressions if that is fitting)
  • each part of the expression (after ? and after : ) should return a value without side effects (the expression x = true returns true as all expressions return the last value, but it also changes x without x having any effect on the returned value)

In short - the 'correct' use of a ternary expression is

var resultofexpression = conditionasboolean ? truepart: falsepart;

Instead of your example condition ? x=true : null ;, where you use a ternary expression to set the value of x, you can use this:

 condition && (x = true);

This is still an expression and might therefore not pass validation, so an even better approach would be

 void(condition && x = true);

The last one will pass validation.

But then again, if the expected value is a boolean, just use the result of the condition expression itself

var x = (condition); // var x = (foo == "bar");

UPDATE

In relation to your sample, this is probably more appropriate:

defaults.slideshowWidth = defaults.slideshowWidth || obj.find('img').width()+'px';
2 of 13
32

No, it needs three operands. That's why they're called ternary operators.

However, for what you have as your example, you can do this:

if(condition) x = true;

Although it's safer to have braces if you need to add more than one statement in the future:

if(condition) { x = true; }

Edit: Now that you mention the actual code in which your question applies to:

if(!defaults.slideshowWidth)
    { defaults.slideshowWidth = obj.find('img').width()+'px'; }
🌐
Reddit
reddit.com › r/learnjavascript › ternary without else
r/learnjavascript on Reddit: Ternary without else
June 2, 2023 -

I have scoured StackOverflow regarding this issue and the suggested solution seems to be to use the && operator instead of a ternary statement like so condition && (x = true); , however this doesn't work when I use it in my code:

let a = false

b = a && 3

console.log(b)

This logs false instead of undefined which is what I expected since a is false.

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Conditional_operator
Conditional (ternary) operator - JavaScript | MDN
The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark (?), then an expression to execute if the condition is truthy followed by a colon (:), and finally the expression to execute if the condition is falsy.
🌐
SitePoint
sitepoint.com › javascript
Ternary operator - JavaScript - SitePoint Forums | Web Development & Design Community
August 22, 2010 - The question is a theoretical one regarding the JavaScript syntax for ternary operator. Now, in conclusion I think that the following syntaxes works but isn’t a good solution: ... There is difference of semantics in regards to ternary operators. It’s better when they are used to just to assign values. If you want to run a different set of statements or functions, staying with the if/else structure is a better choice.
🌐
DEV Community
dev.to › dailydevtips1 › javascript-if-shorthand-without-the-else-13ch
JavaScript if shorthand without the else - DEV Community
August 1, 2022 - However, sometimes we might want to execute or set something if a specific condition is met. Let's look at the following example. ... This piece of code should alert the user if the our_value condition is truthy. There is no else involved.
🌐
Daily Dev Tips
daily-dev-tips.com › posts › javascript-if-shorthand-without-the-else
JavaScript if shorthand without the else
August 1, 2022 - However, sometimes we might want to execute or set something if a specific condition is met. Let’s look at the following example. ... This piece of code should alert the user if the our_value condition is truthy. There is no else involved.
🌐
Mblogs
mbloging.com › post › learn-how-to-use-the-javascript-ternary-operator-effectively-without-else
Master JavaScript Ternary Operator: Usage Without Else | Mbloging
Single-Line Conditionals: Best ... statements. ... The ternary operator is a shorthand for conditional evaluations, replacing traditional if-else statements with a more concise format....
🌐
Built In
builtin.com › software-engineering-perspectives › ternary-operator-js
Ternary Operator in JavaScript Explained | Built In
It is written as “?:” and takes three operands; a logical condition, a value to return if true, and a value to return if false. But it’s common knowledge among developers that if/else statements with lots of conditions can get messy.
Find elsewhere
🌐
EyeHunts
tutorial.eyehunts.com › home › javascript ternary operator without else condition | is it possible?
JavaScript ternary operator without else condition | Is it Possible?
April 16, 2021 - A ternary operation is called ternary ... It’s an expression returning a value. If you omit the else you would have an undefined situation where the expression would not return a value....
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript tutorial › javascript ternary operator
JavaScript Ternary Operator
November 15, 2024 - In this example, the returned value of the ternary operator is the last value in the comma-separated list. ... If the locked is 1, then the canChange variable is set to false, otherwise, it is set to true. In this case, you can simplify it by using a Boolean expression as follows: let locked = 1; let canChange = locked != 1;Code language: JavaScript (javascript)
🌐
ESLint
eslint.org › docs › latest › rules › no-ternary
no-ternary - ESLint - Pluggable JavaScript Linter
This rule disallows ternary operators. ... /*eslint no-ternary: "error"*/ const foo = isBar ? baz : qux; function quux() { return foo ? bar() : baz(); } ... /*eslint no-ternary: "error"*/ let foo; if (isBar) { foo = baz; } else { foo = qux; } function quux() { if (foo) { return bar(); } else { return baz(); } }
🌐
Programiz
programiz.com › javascript › ternary-operator
JavaScript Ternary Operator (with Examples)
In JavaScript, a ternary operator can be used to replace certain types of if..else statements.
🌐
freeCodeCamp
freecodecamp.org › news › the-ternary-operator-in-javascript
JavaScript Ternary Operator – Syntax and Example Use Case
January 6, 2023 - You cannot use the operator without assigning the returned value to a variable: const result = condition ? trueExpression : falseExpression · The returned value depends on the evaluation of the condition expression.
🌐
SitePoint
sitepoint.com › blog › javascript › quick tip: how to use the ternary operator in javascript
Quick Tip: How to Use the Ternary Operator in JavaScript — SitePoint
November 6, 2024 - In this tutorial, we’ll explore ... operator (also known as the conditional operator) can be used to perform inline condition checking instead of using if...else statements....
🌐
Refine
refine.dev › home › blog › tutorials › how to use the javascript ternary operator
How to Use the JavaScript Ternary Operator | Refine
October 8, 2024 - I wanted to share a few quick insights ... code and short-circuit logic. It’s a quick way to check a condition and return something based on it without writing an if/else block....
Top answer
1 of 16
176

Personally I find the best way to do this is still the good old if statement:

var value = someArray.indexOf(3);
if (value === -1) {
  value = 0;
}
2 of 16
109

Code should be readable, so being succinct should not mean being terse whatever the cost - for that you should repost to https://codegolf.stackexchange.com/ - so instead I would recommend using a second local variable named index to maximize reading comprehensibility (with minimal runtime cost too, I note):

var index = someArray.indexOf( 3 );
var value = index == -1 ? 0 : index;

But if you really want to cut this expression down, because you're a cruel sadist to your coworkers or project collaborators, then here are 4 approaches you could use:

1: Temporary variable in a var statement

You can use the var statement's ability to define (and assign) a second temporary variable index when separated with commas:

var index = someArray.indexOf(3), value = index !== -1 ? index: 0;

2: Immediately-Invoked Function Expression (IIFE)

Another option is an anonymous function which is invoked immediately after it’s defined:

// Traditional syntax:
var value = function( x ) { return x !== -1 ? x : 0 }( someArray.indexOf(3) );

// ES6 syntax:
var value = ( x => x !== -1 ? x : 0 )( someArray.indexOf(3) );

3: Comma operator

There is also the infamous "comma operator" which JavaScript supports, which is also present in C and C++.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator

You can use the comma operator when you want to include multiple expressions in a location that requires a single expression.

You can use it to introduce side-effects, in this case by reassigning to value:

var value = ( value = someArray.indexOf(3), value !== -1 ? value : 0 );

This works because var value is interpreted first (as it's a statement), and then the left-most, inner-most value assignment, and then the right-hand of the comma operator, and then the ternary operator - all legal JavaScript.

4: Re-assign in a subexpression

Commentator @IllusiveBrian pointed out that the use of the comma-operator (in the previous example) is unneeded if the assignment to value is used as a parenthesized subexpression:

var value = ( ( value = someArray.indexOf(3) ) !== -1 ? value : 0 );

Note that the use of negatives in logical expressions can be harder for humans to follow - so all of the above examples can be simplified for reading by changing idx !== -1 ? x : y to idx == -1 ? y : x - or idx < 0 ? y : x.

var value = ( ( value = someArray.indexOf(3) ) == -1 ? 0 : value );
Top answer
1 of 16
771

This is a one-line shorthand for an if-else statement. It's called the conditional operator.1

Here is an example of code that could be shortened with the conditional operator:

var userType;
if (userIsYoungerThan18) {
  userType = "Minor";
} else {
  userType = "Adult";
}

if (userIsYoungerThan21) {
  serveDrink("Grape Juice");
} else {
  serveDrink("Wine");
}

This can be shortened with the ?: like so:

var userType = userIsYoungerThan18 ? "Minor" : "Adult";

serveDrink(userIsYoungerThan21 ? "Grape Juice" : "Wine");

Like all expressions, the conditional operator can also be used as a standalone statement with side-effects, though this is unusual outside of minification:

userIsYoungerThan21 ? serveGrapeJuice() : serveWine();

They can even be chained:

serveDrink(userIsYoungerThan4 ? 'Milk' : userIsYoungerThan21 ? 'Grape Juice' : 'Wine');

Be careful, though, or you will end up with convoluted code like this:

var k = a ? (b ? (c ? d : e) : (d ? e : f)) : f ? (g ? h : i) : j;

1 Often called "the ternary operator," but in fact it's just a ternary operator [an operator accepting three operands]. It's the only one JavaScript currently has, though.

2 of 16
183

I want to add some to the given answers.

In case you encounter (or want to use) a ternary in a situation like 'display a variable if it's set, else...', you can make it even shorter, without a ternary.


Instead of:

var welcomeMessage  = 'Hello ' + (username ? username : 'guest');

You can use:

var welcomeMessage  = 'Hello ' + (username || 'guest');

This is Javascripts equivallent of PHP's shorthand ternary operator ?:

Or even:

var welcomeMessage  = 'Hello ' + (username || something || maybethis || 'guest');

It evaluates the variable, and if it's false or unset, it goes on to the next.