This question already has answers here:
Why don't logical operators (&& and ||) always return a boolean result?
(9 answers)
All falsey values in JavaScript
(4 answers)
Closed 5 years ago.
I am trying to figure out why this javascript function returns 0 when this.position.startOffset value is 0 but not when the value is a number other than 0.
ready: function() {
return (this.position.startOffset
&& this.position.endOffset
&& this.position.representation.trim().length >= 0
&& this.text.id
&& this.user.id
&& this.concept);
}
The && chain will stop evaluating at the first non-truthy (falsy) value and return it. Since 0 is falsy it is returned when it is encountered. If no falsy value is encountered then the last value is returned:
var a = 55 && [] && 0 && false && true; // yeild 0 as it is the first falsy value to encounter
console.log("a:", a);
var b = 30 && true && [] && "haha"; // yeild "haha" as it is the last value (no falsy value encountered)
console.log("b:", b);
Falsy values are:
null
undefined
the empty string '' or ""
the number 0
boolean false
NaN
This happens due to how javascript handles numbers in boolean expressions.
If the first parameter in a and (&&) statement returns 0, then the right parameters will not be evaluated and 0 will be returned.
Related
This question already has answers here:
Why `null >= 0 && null <= 0` but not `null == 0`?
(6 answers)
Closed 8 months ago.
can someone explain the null comparison with 0
null > 0 // false
null == 0 // false
null > 0 || null == 0 // false
null >= 0 // true
why it happening ?
The reason is that an equality check == and comparisons > < >= <= work differently. Comparisons convert null to a number, treating it as 0. That’s why null >= 0 is true and null > 0 is false.
On the other hand, the equality check == for undefined and null is defined such that, without any conversions, they equal each other and don’t equal anything else. That’s why null == 0 is false.
Source: Comparisons
In Javascript null isn't equal to 0 but equal to undefined.
null == undefined // true
Also :
Strange result: null vs 0
Let’s compare null with a zero:
alert( null > 0 ); // (1) false
alert( null == 0 ); // (2) false
alert( null >= 0 ); // (3) true
Mathematically, that’s strange. The
last result states that "null is greater than or equal to zero", so in
one of the comparisons above it must be true, but they are both false.
The reason is that an equality check == and comparisons > < >= <= work
differently. Comparisons convert null to a number, treating it as 0.
That’s why (3) null >= 0 is true and (1) null > 0 is false.
On the other hand, the equality check == for undefined and null is
defined such that, without any conversions, they equal each other and
don’t equal anything else. That’s why (2) null == 0 is false.
Comparisons
This question already has answers here:
What does "!" operator mean in javascript when it is used with a non-boolean variable?
(5 answers)
Closed 2 years ago.
I looked a lot, but I couldn't find an answer for this especific case.
Why does this expression return true?
let variable = 0
!variable // true
I understand that the ! mark checks if a value is null or undefined, but in this case variable is defined. This is tricking me.
Isn't 0 really considered a valid value?
! is known as the logical NOT operator. It reverses the boolean result of the operand (or condition)
0 is also considered as the boolean false, so when you use !variable you are using the logical operator and saying it to change the value of the variable to its opposite, that in boolean is true
0 == false == !1 == !true
1 == true == !0 == !false
in Javascript are considered false:
false, null, undefined, "", 0, NaN
are considered true:
true, 1, -0, "false". <- the last one is a not empty string, so its true
if( false || null || undefined || "" || 0 || NaN) //never enter
if( true && 1 && -1 && "false") //enter
https://developer.mozilla.org/en-US/docs/Glossary/Falsy
To quote MDN Web docs, the Logical NOT !:
Returns false if its single operand can be converted to true; otherwise, returns true
So in your case it returns true because 0 can be converted to false
You can check out this link:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators
This question already has answers here:
Why don't logical operators (&& and ||) always return a boolean result?
(9 answers)
Closed 4 years ago.
I had checked for many test cases. It's looking if the first value is true and it's returning the second value.
Example:
2 && 5 // --> 5
5 && 10 // --> 10
0 && 2 // --> 0
Why doesn't it return either True or 1?
Per https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Description,
Operator: Logical AND (&&)
Usage: expr1 && expr2
Returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands are true; otherwise, returns false.
The && operator will return the last value if all other values are truthy, otherwise it will return the first non truthy value. So, as in 0 && 2, 0 is non-truthy, that gets returned.
Why does the expression 1 && 2 evaluate as 2?
console.log("1 && 2 = " + (1 && 2));
&& (and operator) returns the last (right-side) value as long as the chain is "truthy".
if you would try 0 && 2 -> the result would be 0 (which is "falsy")
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Logical_operators
According to MDN:
expr1 && expr2 returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands are true; otherwise, returns false.
Because 1 can be evaluated to true, 1 && 2 returns 2.
According to this page:
Why 1 && 2 == 2
AND returns the first falsy value or the last value if none were found.
OR returns the first truthy one.
For multiple operators in same statement:
precedence of the AND && operator is higher than OR ||, so it executes before OR.
alert( 5 || 1 && 0 ); // 5
Because its and, all the values need to be evaluated only up to the first 0, and the value of the last evaluation is returned, and it's the last one.
Although not directly relevant here, note that there's a difference between bitwise and logical operators. The former tests bits that are the same and only return those, the latter reduces to only true (!=0) or false (=0), so contrary to intuition, bitwise AND and AND are not interchangable unless the values are exactly 0 or 1.
This question already has answers here:
JavaScript OR (||) variable assignment explanation
(12 answers)
Closed 6 years ago.
Why the console shows "2" and "false" in this expressions?
var a = '' || 0 || 2 || true || false;
var b = 3 && true && false && null;
console.log (a,b);
You got those results because it's logical comparison here, using logical operators && and ||, if you take a look at MDN Specification of Logical Operators, you will see that:
Logical OR (||): Returns expr1 if it can be converted to true; otherwise, returns expr2. Thus, when used with Boolean values, || returns true if either operand can be converted to true; if both can be converted to false, returns false.
Logical AND (&&): Returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands can be converted to true; otherwise, returns false.
So in your case:
For the first expression:
var a = '' || 0 || 2 || true || false;
It will return 2 because it's the first oprand that's evaluated to true.
And for the second one:
var b = 3 && true && false && null;
It will return false as one of its operand is false.