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? https://1921681254.mx/

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.

2 Likes

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.

EDIT

Another example :smile:

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

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.