56

So I'm using a shorthand JavaScript if/else statement (I read somewhere they're called Ternary statements?)

this.dragHandle.hasClass('handle-low') ? direction = "left" : direction = "right"

This works great, but what if later I want to use just a shorthand if, without the else portion. Like:

direction == "right" ? slideOffset += $(".range-slide").width()

Is this possible at all?

2
  • 4
    The technical term for these fragments are conditional expressions, which use the conditional operator_ ?:. Because this operator takes three operands, it is called a ternary operator. Commented Jul 19, 2012 at 5:36
  • 2
    2 times faster jsperf.com/speed-test-for-conditions Commented Jul 4, 2013 at 21:48

11 Answers 11

104

you can use && operator - second operand expression is executed only if first is true

direction == "right" && slideOffset += $(".range-slide").width()

in my opinion if(conditon) expression is more readable than condition && expression

Sign up to request clarification or add additional context in comments.

5 Comments

will probably stick with if(condition) expression - but good to know the && operator works in this situation!
I have used before double quotes and worked... something like >>> direction == "right" ? slideOffset += $(".range-slide").width() : "" .... so does this make any problems?
Is there a name for this type of inline assignment?
slideOffset += direction == "right" && $(".range-slide").width() also works, and keeps the operator at the beginning.
@ewahner It's called an "antipattern" :-)
9

Don't think of it like a control-block (ie: an if-else or a switch). It's not really meant for running code inside of it.

You can. It just gets very ugly, very fast, which defeats the purpose.

What you really want to use it for is ASSIGNING VALUES.

Taking your initial example and turning it on its head a little, you get:

direction = (this.dragHandle.hasClass("handle-low")) ? "left" : "right";

See. Now what I've done is I've taken something that would have required an if/else or a switch, which would have been used to assign to that one value, and I've cleaned it up nice and pretty.

You can even do an else-if type of ternary:

y = (x === 2) ? 1 : (x === 3) ? 2 : (x === 4) ? 7 : 1000;

You can also use it to fire code, if you'd like, but it gets really difficult after a while, to know what's going where (see the previous example to see how even assignment can start looking weird at a glance)...

((this.dragHandle.hasClass("...")) ? fireMe(something) : noMe(somethingElse));

...this will typically work.

But it's not really any prettier or more-useful than an if or a branching, immediately-invoking function (and non-JS programmers, or untrained JS programmers are going to crap themselves trying to maintain your code).

1 Comment

"untrained JS programmers are going to crap themselves trying to maintain your code" I couldn't have said it better
5

What you have will not work, but why not just use a one line if statement instead.

if(direction == "right") slideOffset += $(".range-slide").width();

This involves less typing than the method Ray suggested. Of course his answer is valid if you really want to stick to that format.

Comments

5

The conditional operator is not a shorthand for the if statement. It's an operator, not a statement.

If you use it, you should use it as an operator, not as a statement.

Just use a zero value for the third operand:

slideOffset += direction == "right" ? $(".range-slide").width() : 0;

Comments

4

you can use && operator

direction == "right" && slideOffset += $(".range-slide").width()

Comments

2

No, This is not possible, because ternary operator requires, three operands with it.

first-operand ? second-operand (if first evaluates to true) : third-operand (if false)

Comments

1

This doesn't exactly answer your question, but ternaries allow you to write less than you've shown:

direction = this.dragHandle.hasClass('handle-low') ? "left" : "right";

And now that I think about it, yeah, you can do your question too:

slideOffset + direction == "right" ? = $(".range-slide").width() : = 0;

This is a theory. The next time I have an opportunity to += a ternary I will try this. Let me know how it works!

Comments

1

You can use this shorthand:

if (condition) expression

Comments

0

If in some cases you really want to use the if shorthand. Even though it may not be the best option, it is possible like this.

condition ? fireMe() : ""

Looks weird, does work. Might come in handy in a framework like Vue where you can write this in a template.

Comments

0

You can using Short-circuit Evaluation Shorthand. if you want the if condition just write the else condition.

let 
a = 2,
b = a !== 2 || 'ok';

console.log(b);

Comments

0

I had this issue with the shorthand if statement and I've just solved it by putting 0 , "" or null in the third-operand.

this.dragHandle.hasClass('handle-low') ? direction = "left" : 0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.