Lydon Ch
Lydon Ch

Reputation: 8815

Javascript boolean true/false shorthand in jQuery

Looking at jQuery's deferred.js source code,

} else if ( !( --remaining ) ) {
    deferred.resolveWith( contexts, values );
}

What does !( --remaining) comparison do ?

From https://stackoverflow.com/a/4943788/115988, it looks like it's a 'confusing' shorthand boolean check?

Upvotes: 0

Views: 311

Answers (3)

Lydon Ch
Lydon Ch

Reputation: 8815

Much easier to read version of !(--remaining), will need to profile the performance.

                } else if( remaining > 0 ) {
                    --remaining;

                    // if --remaining === 0, then resolve Deferred
                    if( remaining === 0 ) {
                        deferred.resolveWith( contexts, values );
                    }
                }

Upvotes: -1

dda
dda

Reputation: 6203

!(--remaining):

! Not True test

-- decrement

--> decrement remaining and then test whether it's == 0.

Upvotes: 0

Naftali
Naftali

Reputation: 146302

It is checking that value of remaining is not equal to 0 (while also decrementing the value).

Essentially it is doing this:

...
else if ( remaining -= 1 && !( remaining ) ) { 
    deferred.resolveWith( contexts, values );
}

Upvotes: 3

Related Questions