0

I use the ternary operator but often want to check things without an else clause. I'm wondering if there's any short way to omit this.

I've found that

a ? b : {}

works as a replacement of if (a) {b}, saving 2 characters, but not

a ? b :

which I guess does makes sense, it being a ternary operator.

My only thought is that those empty curly brackets are a bit unsightly and is there any other way to specify a blank : statement?

Alternatively, is there a binary ? operator? I've rummaged in the spec but can't see how I'd use a binary logical operator, and I guess that's what if is for.

Is there some way to use a binary logical operator to express a conditional or shorthand for {} ? I know this is probably the height of laziness for 3 characters but I'm just curious if anyone has any tricks to share.

Community
  • 1
  • 1
Louis Maddox
  • 5,226
  • 5
  • 36
  • 66
  • 1
    You could do `a && b()`, assuming b is a function call. Not a fan of that though. – Evan Trimboli May 04 '14 at 13:44
  • It's more convenient to write a ternary but is much more slower then if-else. – radubogdan May 04 '14 at 13:45
  • 1
    @radubogdan: What do you mean by "much more slower"? – Bergi May 04 '14 at 13:47
  • @Bergi it's not an exact duplicate of `if else` shorthand it's ternary operator shorthand ¬_¬ Ppl too quick to shut off questions on here – Louis Maddox May 04 '14 at 13:51
  • @lmmx: Please read that question and the answers. You ask for an `if` shorthand, the other question asks for "*Can I write the 'if else' shorthand without the else?*"! So what's the difference? – Bergi May 04 '14 at 13:53
  • @radubogdan I [don't think that's right](http://stackoverflow.com/questions/9745389/is-the-ternary-operator-faster-than-an-if-condition), and in terms of writing and reading it definitely ain't. – Louis Maddox May 04 '14 at 13:54
  • @Bergi I admit they are similar but OP doesn't refer to ternary operators by name, so people looking for what I was won't find it easily. People searching for `if else` shorthand will find the ternary operator from that answer, people looking for ternary operator shorthand will find this... That thread makes no mention of guard constructs which seems to be what I'm asking for and which Javascript lacks. – Louis Maddox May 04 '14 at 14:10
  • `&&` is in the top answer on that question. Don’t do this, by the way. – Ry- May 04 '14 at 14:16
  • Ya, just wondering. The downvote suggested it's hardly best practice lol – Louis Maddox May 04 '14 at 14:45
  • @Bergi The speed difference will be negligible but the difference is... http://stackoverflow.com/questions/17328641/ternary-operator-is-twice-as-slow-as-an-if-else-block and http://stackoverflow.com/questions/11368948/are-ternary-statements-faster-than-if-then-else-statements-in-javascript and – radubogdan May 04 '14 at 15:00
  • @radubogdan: That's only the case if you're doing different things with if/else than with the ternary operator. See that C# question you linked for the why. However, the OP plans to use the ternary instead of an if/else, not using its return value; which should then make absolutely no speed difference – Bergi May 04 '14 at 15:49

2 Answers2

5

Your code has a couple of issues:

  • Don't do that; it's confusing

  • {} is not an empty block; it's an object literal. "" or 0 or 42 would have the same effect.

  • The && operator will do what you want.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
2

In a ternary ? : expression, the subsections are not statements, they're expressions. I've never seen {} used; it's OK, but literally any non-empty expression would work:

a ? b : 0

a ? b : false

a ? b : null

a ? b : 2 + 2

This really isn't the clearest way to code simply logic like testing a simple condition. An if statement is clearer, in my opinion, and you don't have the uncomfortable issue of having to pick a meaningless expression just to fill out the syntactic requirements.

This is also idiomatic, though (again, opinion) not terribly clear:

a && b;

That mimics a construct called a "guard" that's present in some languages.

Pointy
  • 405,095
  • 59
  • 585
  • 614