-3

I have the next condition in Java Script:

if(a === 10) {
  getName()
  getLastName()
} 

I want to write this code using ternary operator, and i wrote this:

a === 10 && (getName() && getLastName())

But this does not work.
Question: What could be the issue with my code?

Asking
  • 3,487
  • 11
  • 51
  • 106
  • What returns `getName`? – Nikita Madeev Jun 01 '20 at 12:59
  • please add the wanted result and the missing pieces. – Nina Scholz Jun 01 '20 at 13:00
  • 5
    "*I want to write this code using ternary operator*" - [don't](https://stackoverflow.com/q/29659521/1048572). Also that's not the ternary conditional operator, it's an AND operator. – Bergi Jun 01 '20 at 13:00
  • Is there any possibility that `getName` return empty `string`. In JS empty `string` is considered as false – Math10 Jun 01 '20 at 13:01
  • @NikitaMadeev, it is a function, does not matter, i tried to simulate my issue. And i want to see if it is possible to add many result in ternary operator, like these 2 functions for example. There could be anything. – Asking Jun 01 '20 at 13:01
  • 1
    @AskMen return value affects operator `&&` – Nikita Madeev Jun 01 '20 at 13:02
  • if `getname()` returns `true` only then `getlastname()` would be called - this is what your 1-line code means – Sowjanya R Bhat Jun 01 '20 at 13:03
  • The syntax for a ternary operator can easily be found by googling "javascript ternary operator". Please read one of the many explanations and examples available on the web. – LeGEC Jun 01 '20 at 13:05
  • This is not a ternary operator. It's not even the conditional operator – VLAZ Jun 01 '20 at 13:12

1 Answers1

0

I want to write this code using ternary operator

That's not a ternary operator (an operator accepting three operands), it's the &&, which is a binary operator (an operator accepting two operands). The only ternary operator in JavaScript right now is the conditional operator (condition ? valueIfTruthy : valueIfFalsy).

The problem is that when you combine things with &&, the right-hand operand is only evaluated if the left-hand operand's value is truthy. I guess getName is returning something falsy, which is why the right-hand operand isn't evaluated (getLastName isn't called). && short-circuits.

Don't rewrite the if. It's perfectly clear and straightforward and easy to debug the way it is. It's not broken.

If you were going to use && instead of if, you'd use the comma operator (a binary operator) for the two operations:

// Please don't do this
a === 10 && (getName(), getLastName());

The comma operator evaluates its left-hand operand, throws the result away, and then evaluates its right-hand operand and takes that result as its result.

But again: Please don't. :-)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • how to return more than one element in `if` statement? – Asking Jun 01 '20 at 13:11
  • @AskMen - That's a completely different question. If you need to return two things from a function, you typically use an object or an array. (That isn't changed by using `&&` instead of `if`.) – T.J. Crowder Jun 01 '20 at 13:12