Learned something cool. In my opinion.

Did you know that there is two other ways to do an “if” statement that’s not a switch statement?

Instead of if (2 === 2) {…do something}; else {…do something};

(2 === 2) && {…do something};

(2 ===2) ? {…do something} : {…do something };

So you can use an && operator to do an equivalent if statement for one outcome. Then you can do a conditional ?: operator if you want.

I’ve known about the conditional operator but I didn’t know you can do something similar with the && operator.

The && operator returns the value on the right if the value on the left is true. It will return the value on the left if the left value is false. The value on the right should always be true in this type of scenario.


It is great, I was going to mention a caveat from the && and also || is that you can not do this yet:

let a=5
a===5 && a=6

i.e variables can not be assigned.

But I just tried:

let a=5
a===5 && (a=6)

And that reassigns. I am not sure whether this is new or not.


Another example :smile:

let a=5
a===4 ? (a=6) : (a=85)