DEV Community

Discussion on: when if statement become AND operator

Collapse
 
ddasb profile image
Damien Da Silva Bregieiro

You're wrong i think.

status && something()
Enter fullscreen mode Exit fullscreen mode

is not equal to

if (status && logSomething()) {};
Enter fullscreen mode Exit fullscreen mode

It is equal to

if (status) {
    return something()
} else {
    return status
}
Enter fullscreen mode Exit fullscreen mode

If you want to assign variables with Logical &&. You can do like this :

let something = "";
status && (something = "This works");
Enter fullscreen mode Exit fullscreen mode

an other example :

console.log(0 && 5)
// will return 0
Enter fullscreen mode Exit fullscreen mode

but

console.log(1 && 5)
// will return 5
Enter fullscreen mode Exit fullscreen mode
Collapse
 
es404020 profile image
es404020

Thanks for taking your time to explain