JavaScript combining && statements in a variable to be true or false

Viewed 16036

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?

12 Answers

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

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

Related