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

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]

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(!![]);

JS 0 === (0 || 6); returns false - why?

0 === (0 || 6) // returns false
false === (false || 6) // returns false
(false === false || false === 6) // returns true
(0 === 0 || 0 === 6) // returns true
console.log( 0 === (0 || 6) );
console.log( false === (false || 6) );
console.log( (false === false || false === 6) );
console.log( (0 === 0 || 0 === 6) );
what gives? I ran across this scenario earlier and don't get it.
0 === (0 || 6) // returns false
It's all about order of operations. That returns false because the parenthesis are evaluated first. So (0 || 6) returns 6 and 0 === 6 is false
false === (false || 6) // returns false
(false === false || false === 6) // returns true
(0 === 0 || 0 === 6) // returns true
The rest are easy to follow after that explanation.
0 === (0 || 6) is equivalent to 0 === 6 which is of course false.
false === (false || 6) is equivalent to false === 6 which is also obviously false.
(false === false || false === 6) is equivalent to true || false which is of course true.
(0 === 0 || 0 === 6) is also equivalent to true || false.
Look up "operator precedence". That'll help you wrap your head around javascript in the future.
First, you should know the operators priority.
Second, you should know how the operators work and what they return.
Here is the documentation you can take it as reference.
console.log( 0 === (0 || 6) );
// (0 || 6) returns 6 because 0 is falsy,
// so it becomes 0 === 6. It is obviously return false.
console.log( false === (false || 6) );
(false || 6) is equals (0 || 6), so we get (false === 6) = false.
console.log( (false === false || false === 6) );
// this is three part. === is prior to ||,
// so we should do (false === false) and (false === 6).
// (false === false) is true and (false === 6) is false,
// so we get (true || false) = true
console.log( (0 === 0 || 0 === 6) );
// (0 === 0) is true and (0 === 6) is false,
// so true || false is true.
By the way, most of the JavaScript operator do type conversion
whether the two sides is not the type the operator want.
Here's something you can take a look:
https://javascript.info/type-conversions
From your question, I am guessing you are not familiar with how AND & OR are used or works.
So,
| stands for bitwise OR
and, & stands for bitwise AND
It is a common method of writing AND as && and OR as ||.
For AND, following are the scenarios:
1 && 1 => 1
1 && 0 => 0
0 && 1 => 0
0 && 0 => 0
For OR, following are the scenarios:
1 || 1 => 1
1 || 0 => 1
0 || 1 => 1
0 || 0 => 0
In the above, 1 can be any non-zero integer or value (but we denote it by one, meaning high).
So, if you write,
0 === ( 0 || 6 ),
then, ( 0 || 6 ) is already true, then, if you compare true with 0, then it is always going to be false.
Whereas, if you you change the above condition as:
0 == ( 0 && 6 ), (EDITTED)
( 0 && 6 ) becomes false, and then 0 == false becomes true.
=== and == are different because the second one just checks if they are equal or not, but it does not check if they are exactly same. === checks if the values are identical, meaning exactly same.
I hope my answer was helpful.

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

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.

Why does javascript accept a decimal as an integer

I have this html:
<input type='number' id='length' step='0.1' min='0'; max='5'>Length
and this Javascript
num=document.getElementById('length').value;
if(num==1 || 2 || 3 || 4|| 5){
num='0'+num;
}
My problem is this: while I only want the code inside the brackets to execute if the number from the input is an integer, it also activates if it detects 0.8 or some other decimal. Any Idea why? How do I fix it? Thanks.
To make sure num is a whole number, without having to define all possibilities, use:
if (num % 1 == 0)
Why:
num==1 || 2 || 3 || 4|| 5
equals to:
(num==1) || 2 || 3 || 4|| 5
so if num is "1" (always a string type), the expression returns true, otherwise 2 (also a truthy value), eventually your if statement always succeeds.
How to fix:
// implicitly converts the string type into number type before comparison
// returns true if it is an integer-like string
num == Math.floor(num)
So you could do it like this:
if (num == Math.floor(num) && num > 0 && num < 6) {
// an integer-like string that meets the requirement [1, 5]
}
But remember, the num is still string type now. If you want a number, do:
num = +num
You should do
if (num == 1 || num == 2 || num == 3 || num == 4 || num == 5)
WRONG - otherwise it will compare 2 with 2 and says it's true for the 4 last 'if' parameters.
CORRECTO - any number in JS is considered as true.
You have to edit the "If" loop:
if (num == 1 || num == 2 || num == 3 || num == 4 || num == 5)

A quick way to test equality of more than 2 values at once?

I was wondering if there was a quick way to test the equality of more than two values in js. Something similar to (= 6 6 6).
In the console, I tried things like...
1 == 1 == 1 == 1
true
2 == 2 == 2 == 2
false
0 == 0 == 0
false
0 == 0 == 0 == 0
true
...which was amusing, but also puzzling.
Is there a quick way of doing this in js?
Thanks.
The reason you got unexpected behavior is because we need to adjust your expectations in js a bit ;) 2 == 2 == 2 == 2 does 3 comparisons, all from left to right. The first comparison is the leftmost 2 == 2, which evaluates to true. After that we get the result of the first comparison being compared to (what is in this case) the 3rd 2. Ie, true === 2, which is false. And finally, we get false === 2, which is also false.
It might help to visualize it as such:
(((2 == 2) == 2) == 2)
I think in general a === b && b === c might be what you're looking for.
EDIT: Ah, and sorry I keep switching out the == for ===. It's just habit. And it's a habit I'd recommend. the === operator doesn't do type casting, so it evaluates the value proper, not a casted version of the value.
It's because true == 1 but true != 2
You can try:
function isEquals() {
var flag = true;
for(var i=1; i<arguments.length; i++) flag = flag && (arguments[i] == arguments[0]);
return flag;
}
isEquals(2,2,2); // true
or:
function isEquals() {
var ar = arguments;
return Array.prototype.every.call(arguments, function(a){return a==ar[0];});
}
Yes you can, but you need to use the "Logical Operators" like the && or || to check more than 1 statement like (x<1 && y>0).
You can use this as a quick easy reference:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators
If you have more than three values, it might be more convenient to create a function for use on an array:
function allEqual(arr) {
return arr.every(function (x, i) {
return i === 0 || x === arr[i - 1];
});
}
allEqual([1, 1, 1])
ES6:
function allEqual(...arr) {
return arr.every((x, i) => i === 0 || x === arr[i - 1]);
}
allEqual(1, 1, 1)
As an addition to #vp_arth's answer you could even add a method to the Array prototype
Array.prototype.isHomogeneous = function(){
return Array.prototype.every.call(this, function(c,i,a){ return c === a[0];})
}
So you could do
[1,2,3].isHomogeneous() = false
[1,1,1].isHomogeneous() = true

Categories