1

So I have a bit of a problem, here is the basic version.

var x = 10;
(x < 5) ? call() : "" ;

function call() {
  alert('Less');
}

I want a way to give the question mark if statement "nothing" to do if it finds something to be false. Is what I am doing a legal move in JavaScript? Is there a better way to give the if "nothing" to do if it finds this false? The console does not catch any errors.

However when I tried:

var x = 10;
(x < 5) ? call() :;

function call() {
  alert('Less');
}

It gave me an Error as well as:

var x = 10;
(x < 5) ? call();

function call() {
  alert('Less');
}

Specifically what I am asking is: Is there a proper syntax to the question mark if to make it work without the else part? Thanks in advance.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Brendan
  • 1,399
  • 1
  • 12
  • 18
  • 3
    Why not just use `if (x < 5) call();` – jfriend00 May 01 '14 at 01:57
  • yeah @jfriend00, that's what I'm going to have to do. – Brendan May 01 '14 at 02:00
  • You don't have to use the `if`, it's just the better way to do it. – jfriend00 May 01 '14 at 02:02
  • I'll take your guy's advice, after all, I asked for it, so not taking it would be stupid. Thanks again everyone. – Brendan May 01 '14 at 02:03
  • No @JakeGould It isnt. Read my question before accusing it of being a duplicate please. I was trying to see if there was no need for the else part of the ternary operator. And that other post uses JQuery, which is not what I want. – Brendan May 01 '14 at 02:10
  • That didn't come up when I was typing it in... Thank you @OneKitten – Brendan May 01 '14 at 02:24
  • The correct name is [*Conditional Operator*](http://ecma-international.org/ecma-262/5.1/#sec-11.12). It is *a* ternary opertaor, not *the* ternary operator. And you can do `x < 5? call() : null;` or any other expression that does nothing for the false case (such as `void 0` or `undefined` or whatever). – RobG May 01 '14 at 03:15

2 Answers2

6

Short-circuit operators || and &&:

x < 5 && call();

But it's poor practice, just use an if without an else:

if (x < 5) {
  call();
}
elclanrs
  • 92,861
  • 21
  • 134
  • 171
0

To do it your way, you have to put the conditional value into a dummy variable and make your function call() return something. This is a little bit messy:

var dummy = (x < 5) ? call() : "";

Like the other answers suggest, don't do this, even if you can. A bad semantic is like jumping off a plane without a parachute. It may be fine but more likely you will crash on something.

So, for what you are asking, the answer is "don't". The correct syntax for a condition is with the keyword if.

Frederik.L
  • 5,522
  • 2
  • 29
  • 41
  • Putting the result in a dummy variable does not change whether or not it is legal syntax, nor is it better than the original code by the OP. – Qantas 94 Heavy May 01 '14 at 02:18
  • Edited, thanks for pointing out that there was an error. I didn't specify that the function should return something. Anyway, I'd never recommend this syntax. This is only to show that there is a way to do it. – Frederik.L May 01 '14 at 02:21