7

Using the traditional if statement I can do this:

if(a===0 || b===0) {console.log('aloha amigo')};

But when I try to do something the same thing with a ternary operator, like this:

a===0 || b===0 && console.log('aloha amigo')

I just get errors about unexpected ||.

According to this answer: Precedence: Logical or vs. Ternary operator, we can do it using

condition1 || condition2 ? do if true : do if false

(Sorry I'm not sure how to call the ? : symbols in this case), but I'm not sure how to get it running using && (It means only run the code if returned true).

I created a codepen to test it easily. Here's the whole code:

var a = 0;
var b = 1;

a===0 || b===0 ? console.log('Works here') : console.log('And here');

a===0 || b===0 && console.log('Doesn\'t work here');

a===0 && console.log('The && works for a single test');

Here's the link

Alex Ironside
  • 4,658
  • 11
  • 59
  • 119
  • *"But when I try to do something the same thing with a ternary operator..."* That's not a ternary operator (an operator accepting three operands). That's two binary operators (`||` and `&&` -- operators accepting *two* operands). JavaScript currently has only one ternary operator: The *conditional operator*: `condition ? ifTrue : ifFalse`. It could have another ternary added at some stage, but for now it has only one. – T.J. Crowder Jul 06 '18 at 09:04

1 Answers1

14

Just take parenthesis to prevent operator precedence of && over ||

(a === 0 || b === 0) && console.log('aloha amigo')

Without parenthesis, you get (now with to show the precedence) a different result.

a === 0 || (b === 0 && console.log('aloha amigo'))
^^^^^^^                                             first evaluation
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  second evaluation
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Ok this works. But why do I need to get it into parenthesis? I'll select as best in 10 min – Alex Ironside Jul 06 '18 at 09:04
  • 4
    @alex.iron - The bigger question is why are you wasting your time making your code less readable, harder to maintain, and more prone to error? **;-)** Just use the `if`. That's what it's for. Even the author of the language, Brendan Eich, calls the `condition && doSomething();` an *abusage* of the `&&` operator. – T.J. Crowder Jul 06 '18 at 09:05
  • 1
    Well I saw it being used by people, it made sense so I started using it. It looks readable though. It's an shorter if statement isn't it? – Alex Ironside Jul 06 '18 at 09:07
  • 4
    @alex.iron - Readability is in the eye of the reader of course. :-) Shorter != better. Leave minification to the minifiers. Instead, write clear, easily modified code. Here, an `if` with a proper block is a couple of characters longer, but much easier to modify down-the-line. Anyway, happy coding! – T.J. Crowder Jul 06 '18 at 09:12
  • @alex.iron - :-) I hope my hyperbole above came across in the friendly spirit it was intended to. Written communication is so hard. Happy coding! – T.J. Crowder Jul 06 '18 at 12:40