If condition resolving as true when value is zero - javascript

My code basically looks like this:
console.log(placeCost) //this returns 0 (the number, not string)
if (!placeCost || placeCost == false || placeCost == "undefined" || placeCost == '') {
console.log("no")
}
else {console.log('yes')}
Results is "no" in the console. Why does this resolve as "true"?

Try using the === operator and don't check for !var if you plan to accept falsy parameters.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness
if (placeCost === false || placeCost === "undefined" || placeCost === '') {
console.log("no")
} else {
console.log('yes')
}

You can refer to comparison operators :
Equality (==)
The equality operator == converts the operands if they are not of the
same type, then applies strict comparison.
So !placeCost (like its more verbose form : placeCost == false) is evaluated to true if placeCost is the 0 number as 0 is converted to the false value.
You want do a strict comparison ?
Use === that performs no conversion :
The identity operator returns true if the operands are strictly equal
(see above) with no type conversion.

if placeCost is 0
then !placeCost become 1, that will leads the condition is true.
if (!placeCost) // condition true when placeCost is 0

Related

OR operator with real numbers

Why is the mid number result of this statement?
(0 || 0.571428571428571 || 1) == 0.571428571428571
How the comparsion works?
Well. Looks like stupid question. For those who works in php where this statement has a different result could be helpfull to know that JS will return number instead bool.
|| is a short-circuiting operator. It evaluates its left-hand operand, and returns that if it's truthy, otherwise it evaluates and returns its right-hand operator. When you have a sequence of || operators, it performs this left-to-right, returning the first truthy value (or the last value if none are truthy).
In your example, 0 is falsey, so 0.571428571428571 is the first truthy value, and it's returned.
Your expression is interpreted as
((0 || 0.571428571428571) || 1)
The result of the first || is truthy, so that's the result of the overall expression. That's how || works: the result is the left-hand operand if it's truthy, or else the right-hand operand.
The answer to this lies in the fact that 0 is considered a false value while non-zero numbers are considered true.
Since || and && will short circuit and return as soon as it knows the outcome. For OR expressions this means that the first truthy value will be returned. For AND expressions it means the first false value is returned:
(true && true && 0 && true) === 0
(false && false && 1) === 1
(false || false || 1 || false) === 1
(false || false || 0) === 0
Since the first value in your expression is a falsey value, it is not returned but the floating point is a truthy value, so it is returned.

0 vs '0' with boolean operators

0 == false and '0' == false are both 'true'
However, (true && 0) is 'false', while (true && '0') is 'true'.
Why?
The abstract comparison (==) rules are described in ES5 11.9.3 while the rules for logical operators (&&) are described in ES5 11.11.
In short, == is just more complex than &&. Where && just uses the internal ToBoolean() to evaluate its operands, == has various conditions that may result in the use of ToBoolean(), ToNumber(), and/or ToPrimitive().
(0 == false) == true:
7. If Type(y) is Boolean, return the result of comparison x == ToNumber(y)
ToNumber(false) === 0, so 0 == 0, so true.
('0' == false) == true:
This also passes through step 7, resulting in '0' == 0.
Then, starting over at the top, it reaching step 5:
5. If Type(x) is String and Type(y) is Number,
return the result of the comparison ToNumber(x) == y.
ToNumber('0') === 0, so again 0 == 0, and again true.
!!(true && 0) == false
&& simply returns the 1st operand if it's falsy (ToBoolean(...) === false), or the 2nd operand.
It's strictly (true && 0) === 0.
And, when used as an if condition, the result (0) will as well be passed through ToBoolean(...) and ToBoolean(0) === false.
!!(true && '0') == true
Again, this returns the 2nd operand, '0'.
This time, however, ToBoolean('0') === true as '0' is a non-empty String, making it truthy.
Also, if you want simpler comparison rules, use strict comparison (===, 11.9.6).
'0' (or any non-empty string) is 'truthy' in JS. The == operator, however, does some strange type-coercion which is why many prominent JS figures including Crockford highly discourage it. This is a good example of why you should avoid it, it takes the string '0' and coerces it into a falsey value.
Here's a link that explains this process:
http://webreflection.blogspot.com/2010/10/javascript-coercion-demystified.html
If Type(x) is Boolean, return the result of the comparison
ToNumber(x) == y: false == 0 and true == 1 but true != 2
So even stranger than your example is this:
('1' == true) // true
('2' == true) // false!

JavaScript string resolves to false

Why both of these resolve to false:
console.log("potato" == false); //false
console.log("potato" == true); //false
Because what I know when using == loose comparison, JS coerces the operand. Since in JS, non-empty string should be true, why do above return false above ?
There needs a clarification about the == operator. From ECMA-262 Section 11.9.3 the rule6, rule7 and later rule4 determines your result
rule 6. If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
rule 7. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
rule 4. If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).
In your context, false and true will be converted to 0 and 1, while the 'potato' will be converted to NaN, the comparison expression's value is always false.
"potato" == false // => "potato" == 0 => NaN == 0
"potato" == true // => "potato" == 1 => NaN == 1
Similarly, in '' == true comparison, '' will be converted to 0 while true will be converted to 1, the expression is false.
'' == false // => 0 == 0
'' == true // => 0 == 1
Due to the non-intuitive implementation of ==,=== is encouraged to use in comparison.
You mean Truthy and Falsy Values:
if ("potato") {
console.log("true")
}
But here you compare the String "potato" and a true, JavaScript will try to cast to a comparable thing and that is not the case when you compare the two values you have.

Why parenthesis don't change the operator priority for == and === in JavaScript?

I'm kind of confused! Why paranthesis don't affect the priority in these statements?
true === '0' == 0 // returns true
(true === '0') == 0 // returns true again!
because true === '0' evaluates to false, and false == 0 is true. (because false and 0 are both "non-truthy")
Remember that === compares strict equality and == tests for equality with conversion.
It's not that the priority is different, it's that both groupings evaluate to true:
true === '0' is false
false == 0 is true
'0' == 0 is true
true === true is true
You might want to review the JS truth table
Because (true === '0') is false and false == 0 is true in both cases.
In other words:
(true === '0') == 0
resolves to
false == 0
Which is true.
Because the operators === and == have the same precedence are are left-to-right associative. Both the expressions result in the same interpretation. Consider the following expressions as for why the result is what it is:
true === '0' // false
// so: true === '0' == 0 is false == 0 and;
// (true === '0') == 0 is (false) == 0 is false == 0 and;
false == 0 // true
Happy coding.
> true === '0'
false
> false == 0
true
(true === '0') == 0
This one evaluates to:
false == 0 // which is true
Because (true === '0') is false and false == 0 is true and in this case JavaScript is nt doing a strict type comparison if you want it to return false change second part to ===
because true === '0' return false and false == 0 return true
Even with the paranthesis, your first check will always return false and
false == 0 will always return true
The === will check if the value is exactly the same (type inculded). So since you compare a char with a boolean, the result will alway be false.
By using parenthesis, you have not changed the order of execution. It was left to right even without parenthesis. The left part true=='0' returns 0.
0 == 0 returns true, so final answer is true both the times.

What is the difference between != and !== operators in JavaScript?

What is the difference between the !== operator and the != operator in JavaScript? Does it behave similarly to the === operator where it compares both value and type?
Yes, it's the same operator like ===, just for inequality:
!== - returns true if the two operands are not identical. This operator will not convert the operands types, and only returns false if they are the same type and value. —Wikibooks
Yes, !== is the strict version of the != operator, and no type coercion is done if the operands are of different type:
0 != '' // false, type coercion made
0 != '0' // false
false != '0' // false
0 !== '' // true, no type coercion
0 !== '0' // true
false !== '0' // true
I was about to post this W3Schools page, but funnily enough it didn't contain this operator!
At least, the !== is indeed the inverse of === which tests the equality of both type and value.

Categories

Resources