7

just a quick question. I cannot find anything relating to this since I don't really see how to explain it... but, if I combine two bool values using an && to make another variable, what will happen?

var is_enabled = isEnabled() && isSupported();

If isEnabled() is false and isSupported() is true, will it equal false?

Anonymous
  • 553
  • 5
  • 17
  • 41
  • 3
    Did you try it? What did it output? – gen_Eric Jul 18 '12 at 16:02
  • If your functions are really returning the boolean constants `true` or `false`, then yes. – Pointy Jul 18 '12 at 16:03
  • 4
    Q: If isEnabled() is false and isSupported() is true, will it equal false? A: You're kidding, right? Q: Did you *try* it? Q: Can you think of any possible reason it *wouldn't* be false??? – paulsm4 Jul 18 '12 at 16:03
  • I cannot try it as I am on mobile atm but this question came to mind. Basically my question was asking if I could combine the two functions with an && without error – Anonymous Jul 18 '12 at 16:08
  • possible duplicate of [Logical operators in JavaScript — how do you use them?](http://stackoverflow.com/questions/4535647/logical-operators-in-javascript-how-do-you-use-them) –  Jul 18 '12 at 16:15

12 Answers12

33

In Javascript the && and || operators are slightly strange. It depends on if the value is "falsy" (zero, undefined, null, empty string, NaN) or truthy (anything else, including empty arrays).

With && if the first value is "falsy", then the result of the operation will be the first value, otherwise it will be the second value. With || if the first value is "falsy" then the result of the operation will be the second value, otherwise it will be the first value.

Example:

var a = 5 && 3; // a will be 3
var a = 0 && 7; // a will be 0

var a = 1 || 2; // a will be 1
var a = 0 || 2; // a will be 2

This is very useful if you want to replace this:

if (x == null){
  x = 5;
}

With:

x = x || 5;

So in short, if isEnabled() is truthy then is_enabled will be set to whatever isSupported() returns. If isEnabled() is falsy, then is_enabled will be set to whatever that falsy value is.

Also as Robert pointed out, there is short-circuiting:

var x = 5 || infinite_loop();
var x = false && infinite_loop();

In both cases, the infinite_loop() call doesn't happen, since the two operations are short-circuited - || doesn't evaluate the second value when the first value is truthy, and && doesn't evaluate the second value when the first value is falsy.

robbrit
  • 17,560
  • 4
  • 48
  • 68
  • But in all cases this kind of syntax is a bad practice correct ? because of code readability – Muhammad Saleh Oct 06 '15 at 08:51
  • I've never heard of it being called bad practice, and IMO it reads just fine. But then again I'm used to seeing this kind of thing. – robbrit Oct 09 '15 at 04:02
4

The result of false && true is false.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Atanas Korchev
  • 30,562
  • 8
  • 59
  • 93
2

If isEnabled() is false and you use && then isSupported() will never be called because the evaulation will short circuit.

Robert
  • 2,441
  • 21
  • 12
1

If any operand of && operator is falsy (false, 0, null, undefined, NaN, "") then is_enabled will be assigned the first falsy value.

If all operands of && operator is not falsy, then the last operand will be assigned to is_enabled.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
1

is_enabled would only be set to true if isEnabled and isSupported are both true. So if isEnabled is false, and isSupported is true, is_enabled would be false.

Carl
  • 324
  • 1
  • 15
1

yes:

<script type="text/javascript">
function isEnabled() {
    return false;
}

function isSupported() {
    return true;
}

var is_enabled = isEnabled() && isSupported();

alert(is_enabled);  // = 'false'
</script>
duncan
  • 31,401
  • 13
  • 78
  • 99
1

if both functions return only true or false, then it just works as a normal && with booleans.

1 && 1 = 1
1 && 0 = 0
0 && 1 = 0
0 && 0 = 0
trainoasis
  • 6,419
  • 12
  • 51
  • 82
1

First of all, && is only true if and only if both expressions are true.

So back to your question, true && false will equal to false, so yes.

You can also try to test these expressions yourself using the console function on firebug or chrome developer tools.

gerky
  • 6,267
  • 11
  • 55
  • 82
0

What you can do it's to just add a single & and apply an AND operation to those booleans, making it that if both of them are true, then is_enabled will be true.

var is_enabled = isEnabled() & isSupported();

EDIT Thanks to Pointy to pointing out that my syntax is incorrect, this should apply to C language, guess i just got confused

Isaac Gonzalez
  • 1,734
  • 1
  • 16
  • 22
  • `&&` is also an `AND` operation. `&` is a bitwise `AND`. – gen_Eric Jul 18 '12 at 16:04
  • 2
    It should be noted that the semantics of `&` are significantly different from `&&`, particularly in that `&` always evaluates both expressions while `&&` only evaluates the right-hand side when the left-hand side is `true`. – Pointy Jul 18 '12 at 16:04
  • 1
    Also (had to check the spec to make sure :-) the bitwise-logical operators always return an integer value, not boolean. – Pointy Jul 18 '12 at 16:07
0

Here you can see what are the possible cases of tests:

var str='My dummy string';
var str2='My other dummy string';
var falsy1= 1==2;
var truthy1= true;
var truthy2= true;
var falsy2= false;

then:

console.log('Both bool true:', truthy1 && truthy2); // <== Both bool true: true
console.log('Bool true and string:', truthy1 && str); // <== Bool true and string: My dummy string
console.log('String and bool true:', str && truthy1); // <== String and bool true: true
console.log('Falsy operation and string:', falsy1 && str); // <== Falsy operation and string: false
console.log('Bool false and string:', falsy2 && str); // <== Bool false and string: false
console.log('Both bool false and true:', falsy1 && truthy1); // <== Both bool false and true: false
console.log('Both bool true and false:', truthy1 && falsy1); // <== Both bool true and false: false
console.log('Operation false and bool true:', falsy2 && truthy1); // <== Operation false and bool true: false
console.log('Operation false and bool false:', falsy2 && falsy1); // <== Operation false and bool false: false
console.log('Both strings:', str2 && str); // <== Both strings: My dummy string
console.log('Both strings:', str && str2); // <== Both strings: My other dummy string   
console.log('String and bool false:',  str && falsy1); // <== String and bool false: false  
console.log('String and 2 bool false and true:',  str && falsy1  && truthy1); // <== String and 2 bool false and true: false
console.log('3 bool false and true and true:',  falsy1 && truthy1 && truthy2); // <== 3 bool false and true and true: false
console.log('3 bool false and false and true:',  falsy1 && falsy1 && truthy1); // <== 3 bool false and false and true: false
console.log('Bool false and operation false and bool true:',  falsy1 && falsy2 && truthy1); // <== Bool false and operation false and bool true: false
console.log('3 bool true and true and false:',  truthy2 && truthy1 && falsy1); // <== 3 bool true and true and false: false
console.log('String and 2 bool false and true:',  str && falsy1 && truthy1); // <== String and 2 bool false and true: false
console.log('String and 2 bool true and false:',  str && truthy1 && falsy1); // <== String and 2 bool true and false: false
console.log('2 bool false and true and string:',   falsy1 && truthy1 && str); // <== 2 bool false and true and string: false
console.log('2 bool true and false string:',  truthy1 && falsy1 && str); // <== 2 bool true and false string: false
console.log('Bool true and string and bool false:',  truthy1 && str && falsy1); // <== Bool true and string and bool false: false
console.log('Bool false and string and bool true:',  falsy1 && str && truthy1); // <== Bool false and string and bool true: false

And the bonus:

console.log('The existence of a string:',  !!str); // <== The existence of a string: true
gildniy
  • 3,528
  • 1
  • 33
  • 23
0

Short Answer: If first is not falsy then second, else first.

example : if isEnabled() returns false, then false is the result.

otherwise If isEnabled() is true, then whatever isSupported() returns is the result.

Now false and true were used to simplify the answer, false can be any falsy value.

Examples of truthy and falsy values:

var string = ""; // <-- falsy

var filledString = "some string in here"; // <-- truthy

var zero = 0; // <-- falsy

var numberGreaterThanZero // <-- truthy

var emptyArray = []; // <-- truthy, we'll explore more about this next

var emptyObject = {}; // <-- truthy

Ramesh Pareek
  • 1,601
  • 3
  • 30
  • 55
-3

The result of a successful Boolean operation such as "&&" is a Boolean value. As such, the result of isEnabled() && isSupported() will be a Boolean value which will then be assigned to is_enabled