DEV Community

es404020
es404020

Posted on

when if statement become AND operator

I was going through a tutorial recently and I found out this developer kept using && operator to perform if statement. Take a look.

const status  = true

function logSomething(){

console.log('hello world');

}

=========if way========================


if(status) logSomething();

===================and way ======================

status && logSomething();

Enter fullscreen mode Exit fullscreen mode

They do the same thing .Thanks for reading

Discussion (4)

Collapse
bqardi profile image
Sune Seifert

Yeah, it's called "short circuit".

I am taking my liberty to explain this concept in detail, since I get the feeling that this is new to you (appologies if I'm wrong):

It's just an evaluation of something / a condition. The status && logSomething() -part is evaluated.
Actually the whole thing evaluates to false because a function that you don't explicitly return something from, will actually (implicitly) return undefined which is a falsy value and therefore evaluates to false.

Therefore you can do something like this:

if (status && logSomething()) console.log("Done");
Enter fullscreen mode Exit fullscreen mode

and console.log("Done") would never execute (unless you return true or a truthy value from the function), but you would still see 'hello world' in the console...

So, in the end:

status && logSomething();
Enter fullscreen mode Exit fullscreen mode

is the same as:

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

Be carefull tho'! You can't do this:

let something = "Change me";
status && something = "I am not allowed to do this";
Enter fullscreen mode Exit fullscreen mode

which was the same as:

if (status && something = "I am not allowed to do this") {};
Enter fullscreen mode Exit fullscreen mode

and we (hopefully) know that we can't assign values when checking a condition, since an assignment won't evaluate to true / false.

What we can do in this case:

if (status) something = "I am not allowed to do this";
Enter fullscreen mode Exit fullscreen mode

Hope it gave some insight and better understanding of this concept...

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 Author

Thanks for taking your time to explain