Reputation: 553
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?
Upvotes: 7
Views: 20703
Reputation: 1669
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
Upvotes: 0
Reputation: 3903
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
Upvotes: 0
Reputation: 324
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.
Upvotes: 1
Reputation: 17960
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.
Upvotes: 34
Reputation: 1734
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
Upvotes: 0
Reputation: 6417
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.
Upvotes: 1
Reputation: 30
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
Upvotes: -3
Reputation: 6720
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
Upvotes: 1
Reputation: 31912
yes:
<script type="text/javascript">
function isEnabled() {
return false;
}
function isSupported() {
return true;
}
var is_enabled = isEnabled() && isSupported();
alert(is_enabled); // = 'false'
</script>
Upvotes: 1
Reputation: 56809
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
.
Upvotes: 1
Reputation: 2471
If isEnabled() is false and you use && then isSupported() will never be called because the evaulation will short circuit.
Upvotes: 2