29

I'm trying to use this and it doesn't appear to be working. I'm guessing it's just not an option, but want to confirm. Is this valid?

(if_it_is) ? thats_cool();
evan
  • 595
  • 2
  • 5
  • 10
  • No, it's not optional. Use a binary operator instead (&&) as long as `thats_cool()` returns a value that evaluates to `true`. – Jonathan M Feb 10 '12 at 18:35
  • Nope. It needs to have the `:` and some value following it. If you want, you could just create a function named `do_nothing()` that...well, does nothing. – Edwin Feb 10 '12 at 18:37
  • 1
    user1048967, That is called `if() {}`. – Dykam Feb 10 '12 at 18:37
  • @Jonathan It doesn't have to return boolean, as it'll be called if `if_it_is` evaluates to a truthy statement. – Edwin Feb 10 '12 at 18:41
  • @Edwin, that's a good catch. Thanks. – Jonathan M Feb 10 '12 at 18:43
  • Dykam - I guess I will have to resort to typing the extra keystroke. wishful thinking I guess! – evan Feb 11 '12 at 07:52
  • Just for the record, aside from the answer Sarfraz wrote below, you don't have to create a null function, it can simply be: (if_it_is) ? thats_cool() : 0; That's a zero, not a winky face with the mouth open. – Kaz Vorpal Jan 30 '16 at 16:51

7 Answers7

56

You can use && there:

if_it_is && thats_cool();

It is basically equal to:

if (your_expression){
   thats_cool();
}
Sarfraz
  • 367,681
  • 72
  • 526
  • 573
  • Nice. You can do null coalescence with or's. var variable2 = variable1 || 'coalesceVal'; – Jason Feb 10 '12 at 18:39
  • 1
    this is clever but it doesn't seem very intuitive to me. It doesn't read well. A question mark, I get. That, no. – evan Feb 11 '12 at 07:53
  • @user1048967: People coming from other languages find it hard, but this is actually beauty and terseness of javascript you need to learn. At first at looks strange to everyone like I myself was confused but now using it effectively. – Sarfraz Feb 11 '12 at 08:48
  • I'm still sad that there's no way to inline a conditional return statement other than `if(x) return y;` because you can't put a return statement in that nor the ternary expression. – Domino Apr 14 '15 at 00:15
  • using jshint it keeps saying that `Expected an assignment or function call and instead saw an expression. (W030) source: 'jshint'` – Muhammad Omer Aslam Oct 09 '17 at 08:10
7

What you are trying to use is a Ternary Operator. You are missing the else part of it.

You could do something like:

(if_it_is) ? thats_cool() : function(){};

or

(if_it_is) ? thats_cool() : null;

Or, as others have suggested, you can avoid the ternary if you don't care about the else.

if_it_is && thats_cool();

In JavaScript, as well as most languages, these logical checks step from left to right. So it will first see if if_it_is is a 'trusy' value (true, 1, a string other than '', et cetera). If that doesn't pass then it doesn't do the rest of the statement. If it does pass then it will execute thats_cool as the next check in the logic.

Think of it as the part inside of an if statement. Without the if. So it's kind of a shorthand of

if (if_it_is && thats_cool()) { }
Marshall
  • 4,548
  • 1
  • 17
  • 13
4

No, it's not valid.

The conditional operator takes the form x ? y : z. The third operand is not like the else in an if, it always has to be there.

Guffa
  • 666,277
  • 106
  • 705
  • 986
3

You can't accomplish that using the ternary operator, but you can use the short-circuit nature of && to do what you want.

(if_it_is) && thats_cool();
driangle
  • 11,351
  • 4
  • 45
  • 51
1

No, you can't. ?: is a ternary operator and MUST have all three of its operands.

Niet the Dark Absol
  • 311,322
  • 76
  • 447
  • 566
1

No, you can't. It's not an "inline if statement", it's the ternary operator, and that is, well… ternary.

(if_it_is) ? thats_cool() : null;

Since you do not seem to be interested in the return value of the operation, you could just write:

if (if_it_is) thats_cool();

though. That would also be better style than using a ternary.

Tomalak
  • 322,446
  • 66
  • 504
  • 612
0

You miss the third component. Should be something like that:

(if_it_is) ? thats_cool() : not_too_cool();

The whole sense is:

condition ? do_something_if_condition_is_true : do_something_if_condition_is_false;
Kath
  • 1,814
  • 15
  • 17