(myVar && foo())
What does the above code mean? What is it equivalent to?
I think it runs on a single line.
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; }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With