views:

157

answers:

5
(myVar && foo())

what does the above code mean? what is it equivalent to?

this is an inline code i think it runs on a single line

+4  A: 

it is an expression that equates to "if myVar is not falsey, run the function foo()". If it's used like this: var x = (myVar && foo()), the result will be:

if myVar is not falsey, x will be set to the output of foo(). if myVar is falsey, then x will be set to the value of myVar.

ithcy
If myvar is truthy x is not set the true, its set to the value of foo(), whatever that might be.
brad
This is not quite right. If myVar is 0, then x is set to 0, not false.
Jason Orendorff
You're both correct. Answer amended.
ithcy
+1  A: 

The expression is making clever use of short circuiting in boolean expressions. foo() will only execute if myVar evaluates to true.

if (myVar) {
    foo();
}

is much clearer. There are probably cases where this sort of code makes sense, but style checkers (if you're interested in that sort of thing) will throw a fit over it. See http://javascript.crockford.com/code.html. It's a good practice to avoid relying on semicolon insertion and expression statements.

edit: Note that var x = a && b(); probably isn't such a bad idea in certain contexts. However, (a && b()) by itself is wrong in several ways. (semicolon insertion, expression statement, aside from being semantically cryptic)

Stuart Branham
i saw this on a jquery script (dialog ui)...
Val
"Exploiting" "stupid" those are some strong words :) If you use JS a lot, it makes sense and is clear as it stands.
Doug Neiner
Grumdrig
Strong words indeed! :) However, if it were written sensibly, this question wouldn't even need to exist, would it? Quoting Crockford, "JavaScript allows any expression to be used as a statement. This can mask some errors, particularly in the presence of semicolon insertion." http://javascript.crockford.com/code.html
Stuart Branham
@Grumdrig Yup! That does read quite well. However, this is an expression statement, and not a very clearly written one at that.
Stuart Branham
Editing answer to seem like less trollbait.
Stuart Branham
ithcy
it is not stupid to ask a question to which the answer you don't know... its stupid to presume or ignore it at all. besides; if i say 1 + 2 = 3, or 2+1 =3 what difference would it make? the answer would always be 3. so i don't know why u insist on using the if statement as a recommended option unless browsers complain at the statement in question
Val
I wasn't calling you stupid. I was calling the snippet you posted out on bad style and practice.
Stuart Branham
bobince
+6  A: 

The expression evaluates to myvar if myvar is falsey, and foo() if myvar is truthy. The following snippets are nearly identical.

var x = (myvar && foo());

if(myvar){ var x = foo(); } else { var x = myvar; }
brad
+3  A: 

The foo() function will only be called if myVar is not falsey: meaning it can't be false, "", 0, null, or undefined. (Any others?)

It's more typical to see an example like this:

window.console && console.log("some helpful debug info");

This is interpreted as follows...

"If the 'window' variable has a member called 'console'..."

(It's important to prefix 'console' with 'window.' because, if console is undefined, you'll get a JavaScript error instead of false.)

"... invoke the 'log' method on the 'console' object."

Drew Wills
*"Any others?"* Just -0 and NaN, I think.
Jason Orendorff
@Jason: `0`, `NaN`, a zero-length string, `undefined`, `null` and of course `false` are *falsy*...
CMS
Ah, thought about NaN as I was writing, but forgot to type it. Thanks!
Drew Wills
A: 

when && are evaluated the left hand is evaluated first i.e. myVar, if this is true then only the right hand side is evaluated i.e. foo(). This is because in a statement A && B if A is false then the expression A&&B will always evaluate to false.

The above statement is usually used in the following way:

var x = myVar && foo();

this can also be written as:

if myVar is truee x = foo(), else x = false
Aly