0

I have the following few lines of code:

toggleMore  : function( $item, show ) {
    ( show ) ? $item.find('a.ca-more').show() : $item.find('a.ca-more').hide(); 
},

What do they mean? Also, how can I address this JSHint warning:

Expected an assignment or function call and instead saw an expression.

Josh Crozier
  • 202,159
  • 50
  • 343
  • 273

1 Answers1

0

It's equivalent to an if{}else{} statement.

Foo = bar ? True : false;

True and false here can be replaced with whatever logic you want to execute

DOfficial
  • 355
  • 2
  • 18
  • is it like an if/else or is it equivalent? – ProfessorManhattan Dec 19 '15 at 00:28
  • Equivalent. It's the shorthand way of writing it – DOfficial Dec 19 '15 at 00:29
  • It's _not_ equivalent to an "if{}else{} statement". It's _not_ a "shorthand way of writing it". The conditional operator is its own beast, with its own nuances and semantics. That it is effectively another way to perform branching is besides the point. – Lightness Races in Orbit Dec 19 '15 at 00:34
  • The big difference is that `if` is a statement, which uses an expression (the condition) and statements (the bits that get executed), while the ternary operator is... an operator, yielding an expression, and using 3 expressions (the condition and the values). You can't write `a = if(this) value1 else value2`, but you can write `a = this?value1:value2`. Conversely, you can write `if (this) break; else continue;`, but you can't write `this?break:continue`. However, you can abuse the ternary operator when your statements are expressions, as is the case in the OP's question. – jcaron Dec 19 '15 at 00:46
  • performance wise, is it ever better to use a ternary operator? – ProfessorManhattan Dec 20 '15 at 01:34