Samaritain Sims
Samaritain Sims

Reputation: 5

What means this kind of expression with logics operators

I have an boolean expression in javascript and i don't know what it means.

a = (b === LEFT && -2 || b === RIGHT && 2 || 0)

Please what does it mean ?

Upvotes: 0

Views: 55

Answers (2)

ajaykumar mp
ajaykumar mp

Reputation: 546

one more shortcut with ternary operator

a = b === LEFT? -2: (b === RIGHT? 2 : 0)

Upvotes: 0

NoNickAvailable
NoNickAvailable

Reputation: 398

The && is a hacky shortcut if:

if (B === LEFT) {
    a = -2;
} else if (B === RIGHT) {
    a = 2;
} else {
    a = 0;
}

Upvotes: 5

Related Questions