Boolean Value of Zero [duplicate] - javascript

This question already has answers here:
Why does ('0' ? 'a' : 'b') behave different than ('0' == true ? 'a' : 'b') [duplicate]
(6 answers)
Closed 8 years ago.
I have the following JavaScript/jQuery code:
if (isVariance && value)
tableCell.addClass('NonZeroVariance');
Where:
isVariance == true and value == "0.00".
(isVariance && value) == "0.00".
(isVariance && !!value) == true.
The if condition evaluates to true, and the class is added to tableCell.
So, my expectation was that zero would be interpreted as false, and that "0.00" would be evaluated as false. But that's not what happens here. Can someone tell me why?

Yes, it appears that value is a string. And while the number 0.00 will evaluate to false, the string "0.00" evaluates to true.
The solution is to first convert the string to a number, and then perform the same test.
value = Number(value);
if (isVariance && value)
tableCell.addClass('NonZeroVariance');
EDIT:
The reason that the string "0.00" evaluates to true, is because: The result is false if the argument is the empty String (its length is zero); otherwise the result is true. - http://javascriptweblog.wordpress.com/2011/02/07/truth-equality-and-javascript/

Related

Why is !!'0' == true when '0' == false [duplicate]

This question already has answers here:
In JavaScript, why is "0" equal to false, but when tested by 'if' it is not false by itself?
(15 answers)
Closed 3 years ago.
The question says it all as I understand !!false = false
Then if
0 == false // true
'0' == false // true
why
!!0 == false
!!'0' == true
string zero ('0') is a truthy value
!!'0' == true
!'0' -> !false -> true
So you're actually doing
true == true
It looks like you stumbled across the importance of the === operator.
'0' == false; // true
'0' === false; // false
Boolean('0'); // true
typeof('0'); // string
typeof(!'0'); // boolean
!'0' === false; // true
!!'0' === false // false
The first negation converts string '0' to a boolean by calling of abstract function ToBoolean. According to JavaScript specification, only 7 values are evaluated/coerced/converted to false i.e are falsy:
null, undefined, NaN, Empty String, +0, -0, and false.
So, '0' is evaluted to a truthy, !'0' to false and !!'0' to true.
PS: Another cases of why '0' == false is evaluated to true is raised after the original question by the OP in a comment below. Even though not relevant to the original post, here is the explanation:
In the specification, section Abstract Equality Comparison reads: "When evaluating x == y, if the type of y is Boolean, first convert y to Number and then do the comparison again".
So Number(false) is evaluated to 0. In the next comparison run, string '0' is compared with number 0 i.e '0' == 0. The spec says convert the string to number and do the comparison again: 0 == 0.

javascript if statement not evaluated as expected [duplicate]

This question already has answers here:
What's the difference between & and && in JavaScript?
(4 answers)
Closed 5 years ago.
Why my "C" condition go to 'else' statement?, they separated are going to 'if' statement but together are not working.
var objTest = {
ID : "10"
};
//A: First Condition: Exist value in property ID
console.log((objTest.ID ? 'if' : 'else')); // output => "if"
//B: Second Condition: Value different from "0"
console.log((objTest.ID != "0" ? 'if' : 'else')); // output => "if"
//C: First and Second Condition together must be "if"
console.log((objTest.ID & objTest.ID != "0" ? 'if' : 'else')); // output => "else"
Your problem is that you are using the wrong ANDopernator, you need to use && instead of &:
console.log((objTest.ID && objTest.ID != "0" ? 'if' : 'else'));
The first &is a bitwise operator and &&is the logical operator.
Please take a look at What's the difference between & and && in JavaScript? for further details.

Difference between != and !== [duplicate]

This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Closed 5 years ago.
In this function, when it is compared the lenght of the array it is used != operator and when it is comparing all elements of the array it is using !== operator. Why?! Thx.
var a = [1,2,3];
var b = [2,3,4];
function equalArrays(a,b){
if(a.length != b.length) return false;
for(var i = 0; i < a.length; i++)
if(a[i] ==! b[i]) return false;
return true;
}
= is an assignment operator, e.g. If you run var x = 1; then x will have the value of 1.
== (or !=) is a comparison operator that checks if the value of something is equal to the value of something else. e.g. if(x == 1) will evaluate to true and so will if(x == true) because 1 will evaluate to true and 0 evaluate to false.
=== (or !==) is another comparison operator that checks if the value of something is equal to the value of, and is the same type as something else. e.g. if(x === 1) will evaluate to true however, if(x === true) will evaluate to false because 1 (the value of x) is an integer and true is a boolean.
The triple equals (===) not only checks the value, but the type.
The following are true:
false == false
false == null
false == undefined
false == 0
2 == "2"
The following are NOT true:
false === null
false === undefined
false === 0
2 === "2"
!= will only check value regardless of operands type. but !== is used to compare both value & type of 2 operands that are being compared to each other.
When its comparing the length of arrays its obvious that both of them are integer so there is no need to compare their types. But in order to compare the elements in the array their types are important. For example assume its comparing string of 5 and integer 5:
if( '5' !== 5 ){
return false
}else{
return true;
}
The above snippet will return false cause two operands are off different types. But this can not be caught by !=, I mean:
if( '5' != 5 ){
return false;
}else{
return true;
}
will return true.
As a rule of thumb, remember that:
JavaScript has both strict and type-converting equality comparison. For strict equality the objects being compared must have the same type and:
Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.
Two numbers are strictly equal when they are numerically equal (have the same number value). NaN is not equal to anything, including NaN. Positive and negative zeros are equal to one another.
Two Boolean operands are strictly equal if both are true or both are false.
Two objects are strictly equal if they refer to the same Object.
Null and Undefined types are == (but not ===). [I.e. (Null==Undefined) is true but (Null===Undefined) is false]
Quoted from: https://stackoverflow.com/a/523647
!==means that two variables are being checked for both their value and their value type (8!==8 would return false while 8!=="8" returns true). != only checks the variable's value (8!=8 and 8!="8" would both return false).
The !== operator returns true when elements are not equal value or not equal type.
Source: W3Schools

Returning && expression from JS function [duplicate]

This question already has answers here:
Javascript AND operator within assignment
(7 answers)
Why don't logical operators (&& and ||) always return a boolean result?
(9 answers)
Closed 6 years ago.
Here is the function I am working with. I actually found it in React Native documentation :
var testFunction = function(word) {
return word && '🍕';
}
Here is how I am using this function :
var testWord = testFunction("Alex");
The final value of testWord, returned by testFunction, is "🍕".
I would have expected the returned value to be either true or false, as the result of the && expression. However the value is a string of value "🍕".
Could someone explain the logic behind this ?
The && evaluates as the right hand side if the LHS is true, otherwise it evaluates as the LHS. It doesn't evaluate as a boolean.
0 && 1 // 0 because 0 is not true
1 && 0 // 0 because 1 is true
1 && 2 // 2 because 1 is true

JavaScript: How '' == '0' // false and 0 == '' //true? [duplicate]

This question already has answers here:
Implied string comparison, 0=='', but 1=='1'
(6 answers)
Closed 7 years ago.
I am curious how
'' == '0' // false
left side is a blank string and right side has string with value 0 so its fine its is false.
But
0 == '' // true
how blank string equals to zero, both are typed different as well as value.
similar
false == 'false' // false
left is false , but we are not equating (===) type of value , its just has value false so it should be true ? but why it is false.
Could we explain the same ?
The left operand is of the type Number.
The right operand is of the type String.
In this case, the right operand is coerced to the type Number:
0 == Number('')
which results in
0 == 0
The following values are always falsy:
false 0 (zero)
"" or '' (empty string)
null
undefined
NaN (a special Number value meaning Not-a-Number!)
I found a nice table explaining what works and what not with the == operator.
Here is the link to the tables also containing === and if().

Categories

Resources