What is "x && foo()"?
Asked Answered
S

5

46

I saw somewhere else said,

x && foo();

 is equal to

if(x){
    foo();
}

I tested it and they really did the same thing.
But why? What exactly is x && foo()?

Strop answered 7/8, 2011 at 2:32 Comment(10)
Even though this is a slick trick, I hope you don't use it. It works but it is considered bad form.Stenotypy
Yeah, I agree with @PortableWorld. if(x){foo();} or even if(x) foo(); on a single line would be just about as comprehensible, more general usage.Gildagildas
But be careful not to confuse this with if( x && foo() ) { ... } which is acceptable and has a different meaning. I, for one, don't mind x && foo(), especially if things like this creep up a lot... JS benefits from minimization.Devaluate
@PortableWorld: it may be considered good form in other languages, however. I use it all the time in Bash (which lacks a one-line if statement), and I think it may be acceptable in Perl as well.Denunciatory
A good use of this idiom is window.console && console.log('xyz'); to keep errant log commands from throwing errors in browsers that don't have the console enabled.Huckaback
For the record I think this is beautiful.Indicia
if(x) foo(); vs x && foo();. Ignoring whitespace, you are saving two characters. Who cares?!Scuffle
I'm with @PortableWorld. Seems like a cheap trick in most cases...Schopenhauerism
@Nick_Wiggill It's more a matter of whether developers after you have to look it up to be certain about it's behavior, as Derek did. Readability issue over whitespace issue.Gildagildas
Duplicate https://mcmap.net/q/243533/-what-a-strange-syntax/377133Friedland
G
70

Both AND and OR operators can shortcut.

So && only tries the second expression if the first is true (truth-like, more specifically). The fact that the second operation does stuff (whatever the contents of foo() does) doesn't matter because it's not executed unless that first expression evaluates to something truthy. If it is truthy, it then will be executed in order to try the second test.

Conversely, if the first expression in an || statement is true, the second doesn't get touched. This is done because the whole statement can already be evaluated, the statement will result in true regardless of the outcome of the second expression, so it will be ignored and remain unexecuted.

The cases to watch out for when using shortcuts like this, of course, are the cases with operators where defined variables still evaluate to falsy values (e.g. 0), and truthy ones (e.g. 'zero').

Gildagildas answered 7/8, 2011 at 2:35 Comment(3)
Check your language's syntax specification to ensure that your language follows the same shortcut execution termination rule. Legacy languages do exist that violate it.Alisha
Actually, in Delphi you can switch it off for the whole project, a specific file, or even around a certain function.. Crazy stuff :) docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/…Virescence
@John: The question specifies javascript…Caramelize
K
25

This is known as short-circuit evaluation.

In this case, if x is False, then foo() doesn't need to be evaluated (the result of && will always be False); if x is True, it does need to be evaluated (even if the result is thrown away).

Karykaryl answered 7/8, 2011 at 2:38 Comment(2)
Ah, the wikipedia article certainly covers it all. As always.Gildagildas
@Tchalvak: It does... if you know what to look for!Karykaryl
E
8

It's not exactly equivalent. The first one is an expression with a return value you can use; the second one is a statement.

If you are not interested in the return value (that is, the information whether both x and foo() evaluate to a truthy value), they are equivalent, but normally, you should use the boolean-logic version only if you want to use it as a boolean expression, e.g.:

if (x && foo()) {
    do_stuff();
}

If you are only interested in running foo() conditionally (when x is truthy), the second form is to be preferred, since it conveys the intention more clearly.

A reason people might prefer the boolean-logic version might be that javascript is subject to an unusual restriction: source code size (more verbose source code means more bandwidth used); since the boolean-logic version uses less characters, it is more bandwidth-efficient. I'd still prefer the more verbose version most of the time, unless the script in question is used a lot - for a library like jQuery, using optimizations like this is perfectly justifyable, but in most other cases it's not.

Enclitic answered 7/8, 2011 at 12:14 Comment(0)
S
3

In javascript, the && operator evaluates left to right and returns the value of the rightmost operation. If the first condition evaluates to false, it doesn't evaluate the second. So its a shorthand of saying "if something is not null or undefined, do something"

Seller answered 7/8, 2011 at 2:35 Comment(0)
D
2

It is short circuiting.

The && operator works like this: It does the logical or of the two operands on both side. If the left hand side has a non zero value then the right hand side is evaluated to determine the truth value. If the left hand side is zero then whatever the right hand side be, the expression will evaluate to 0, therefore the right hand side is not evaluated. So in effect, if x is non-zero then only foo is called, and if x is 0 then foo is not called, and thus, it works like if - else in this case.

Downtrend answered 7/8, 2011 at 2:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.