2

anybody can help me with the following Js syntax? I don't understand the line starting with "( $.inArray( wzdId, this...." I mean why does that line start with just a parentesis? What does it mean?

This is the complete code:

_activateStep: function( wzdId ) {
  if ( condition ) {
    var stepIndex = this._findNav( wzdId ).index(); 
    for( var i = 0; i < stepIndex; ++i) { 
      if( condition ) === -1 ) {
        return;
      }
    }
    ( $.inArray( wzdId, this._activatedSteps ) === -1 ) && this._activatedSteps.push( wzdId );
  }
}

Thank you

  • The parenthesis (`(...)`) in this case are the *grouping operator*. You can use it to change the order of precedence or just make expressions more readable. For example: `6 * (4 + 3)` or `(5 * 2) + (4 * 8)`. – Felix Kling Apr 05 '13 at 17:49

1 Answers1

6

what you have here is

A && B

It uses a common trick based on short-circuit logical operators : B is executed only if A is true.

That's another way to write

if (A) B;

Some people like it because it's a little shorter. It's obviously much less readable too.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • the braces can be around either of the statements. afaik. I often A && (B) myself. – rlemon Apr 05 '13 at 17:36
  • I use the OR (`||`) operator to set defaults like `var a = yourPassVal || 0`, but you have to be careful for when a falsey value is allowed and your default isn't it. – Chad Apr 05 '13 at 17:36
  • and you can do so much fun stuff with it [example from another answer](http://stackoverflow.com/a/15789302/829835) – rlemon Apr 05 '13 at 17:41
  • Clear and quick! Thanks to all of you for your help! – Alessio De Feudis Apr 05 '13 at 17:51