Why 0 === -0 is true, but 1/0 === 1/-0 is false? - javascript

var a = 0;
var b = -a;
When I post the following code to console I got true:
console.log(a === b); // true
But when I do some calculation with it I got false:
console.log(1/a === 1/b); // false
Why is it so?

That is because Infinity == -Infinity is false, as per abstract equality comparison algorithm.
1/0 will yield Infinity at the same time 1/-0 Yields -Infinity. So both are not are not equal and thus returning false.

Related

Why does type conversion for [] and !![] return not the same result?

Why does type conversion for [] and !![] return not the same result?
console.log([] == false); // true
console.log(!![] == false); // false
For the first output: https://262.ecma-international.org/5.1/#sec-11.9.3
[] == false is evaluated as ToPrimitive([]) == ToNumber(false) which is ToNumber([]) == ToNumber(false) and finally 0 == 0.
The conversion chain step by step:
We're starting with [] == false.
Step 1:
If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
Now, we have [] == 0.
Step 2:
If Type(x) is Object and Type(y) is either String or Number,
return the result of the comparison ToPrimitive(x) == y.
Some examples of this conversion chain:
console.log(Number([]));
console.log(Number(false));
console.log([0] == false);
console.log(['0'] == false);
console.log([1] == true);
console.log(['1'] == true);
console.log(['0'] == 0);
console.log([1] == '1');
console.log([1,2,3] == '1,2,3');
For the second output: an array (even empty) is truthy. ![] returns false. !false returns true.
Some examples:
console.log(Boolean([]));
console.log(![]);
console.log(!![]);

How to simplify this boolean / tenary expression?

How would I simplify this ternary expression?
c = a === false && b === false ? true : false;
c is true only when a and b are false.
You don't need ternary expression here. The first expression itself returns are Boolean
c = a === false && b === false
Another trick you can use is compare a !== false with b
c = a !== false === b
Here there are only two values to check. If there are more values then its better use every method.
c = [a,b].every(x => x === false)

JavaScript operator precedence

According to Mozilla, the === operator has higher precedence than the || operator, which is what I would expect.
However, this statement evaluates to the number 1, rather than false.
let x = 1 || 0 === 0; // x === 1;
You have to wrap in parentheses to get a boolean:
let x = (1 || 0) === 0; // x === false;
What is the explanation?
Note: This is not a duplicate of this question, which does not have anything about equality operators - JavaScript OR (||) variable assignment explanation
Higher operator precedence is like a parenthesis around the operands.
let x = 1 || (0 === 0);
The second part gets never evaluated, because of the truthy value of 1
.
|| is a short circuit operator and conditions are evaluated from left to right.
So in left || right, if the left condition is true, the whole condition is evaluated to true and the right one is never evaluated.
In
let x = 1 || 0 === 0; // x === 1;
x = 1 assigns 1 to x and the second condition after || is never evaluated as if (1) is evaluated to true.
And in
let x = (1 || 0) === 0; // x === false;
(1 || 0) is evaluated to true as if (1) is still evaluated to true.
And then true === 0 is evaluated to false.
So x is valued to false.

Why Infinity == Infinity == 1/0 is false?

The question is quite simple:
Infinity == Infinity
>> true
Infinity == 1/0
>> true
Infinity == Infinity == 1/0
>> false
Why the last evaluation is false?
Because Infinity == Infinity == 1/0 is basically
(Infinity == Infinity) == 1/0
so
(true) == 1/0
is false.
its look trying like below..
var d = (2 == 2);
console.log(d) //true
console.log(d == 2); //[true == 2] false
comparison == return always boolean true or false[1 or 0]

Set a default value for a boolean parameter in a javascript function

I have used typeof foo !== 'undefined' to test optional parameters in javascript functions, but if I want this value to be true or false every time, what is the simplest or quickest or most thorough way? It seems like it could be simpler than this:
function logBool(x) {
x = typeof x !== 'undefined' && x ? true : false;
console.log(x);
}
var a, b = false, c = true;
logBool(a); // false
logBool(b); // false
logBool(c); // true
You could skip the ternary, and evaluate the "not not x", e.g. !!x.
If x is undefined, !x is true, so !!x becomes false again. If x is true, !x is false so !!x is true.
function logBool(x) {
x = !!x;
console.log(x);
}
var a, b = false, c = true;
logBool(a); // false
logBool(b); // false
logBool(c); // true

Categories