1

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

1
  • 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). Commented Apr 5, 2013 at 17:49

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

the braces can be around either of the statements. afaik. I often A && (B) myself.
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.
and you can do so much fun stuff with it example from another answer
Clear and quick! Thanks to all of you for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.