JavaScript: Why is []==![]? - javascript

In JavaScript, I noticed that []==![] result is true. demo
I don't understand this result. Can you explain why?

[] is an array, but ![] is a boolean value. Whenever you try to compare two objects with different types with == both objects should be transformed into comparable objects (using ToNumber, see step 7. in 11.9.3). This is why [] == ![] yields true, the first empty array gets evaluated to false.
11.9.3 The Abstract Equality Comparison Algorithm
The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as
follows:
[...]
If x is null and y is undefined, return true.
If x is undefined and y is null, return true.
If Type(x) is Number and Type(y) is String,
return the result of the comparison x == ToNumber(y).
If Type(x) is String and Type(y) is Number,
return the result of the comparison ToNumber(x) == y.
If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
If Type(x) is either String or Number and Type(y) is Object,
return the result of the comparison x == ToPrimitive(y).
If Type(x) is Object and Type(y) is either String or Number,
return the result of the comparison ToPrimitive(x) == y.
Return false.
However, if you use the strict type comparison operator === the result is false, since both types differ:
11.9.6 The Strict Equality Comparison Algorithm
The comparison x === y, where x and y are values, produces true or false. Such a comparison is performed
as follows:
If Type(x) is different from Type(y), return false.
If Type(x) is Undefined, return true.
If Type(x) is Null, return true.
If Type(x) is Number, then
If x is NaN, return false.
If y is NaN, return false.
If x is the same Number value as y, return true.
If x is +0 and y is -0, return true.
If x is -0 and y is +0, return true.
Return false.
If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and
same characters in corresponding positions); otherwise, return false.
If Type(x) is Boolean, return true if x and y are both true or both false; otherwise, return false.
Return true if x and y refer to the same object. Otherwise, return false.

Related

Javascript/jQuery: Check if two values are equal regardless of data type?

I am have an if condition where I need to check if A has the same value as B irrespective of data type, like
if (A == B) {
//body of if statement
}
but the server check in validation will reject if == is present. It will ask to change that to === (but I can't change this).
How I can achieve this, where the possible values and types of A & B are
String, Number, null, '', undefined, NaN
Looks simple but may just be a logic error... :)
Fundamentally, there are valid use cases for == and so I would suggest finding whoever can change that rule and having them do so.
In the absense of that, you can write yourself a function that does what == does, which is called the abstract equality comparison algorithm, and then use that function instead of ==:
The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:
If Type(x) is the same as Type(y), then
Return the result of performing Strict Equality Comparison x === y.
If x is null and y is undefined, return true.
If x is undefined and y is null, return true.
If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).
If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.
If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
If Type(x) is either String, Number, or Symbol and Type(y) is Object, return the result of the comparison x == ToPrimitive(y).
If Type(x) is Object and Type(y) is either String, Number, or Symbol, return the result of the comparison ToPrimitive(x) == y.
Return false.
The exact implementation of every abstract operation used above is clearly defined in the spec.
I found it,(Workaround)
A = A ? A.toString() : A;
B = B ? B.toString() : B;
if(A === B){
//Rock it
}

Why === faster than == in JavaScript? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
See these tests:
== vs ====
== vs ===
Here's what javascript has to do for ===:
If Type(x) is different from Type(y), return false.
If Type(x) is Undefined, return true.
If Type(x) is Null, return true.
If Type(x) is Number, then
If x is NaN, return false.
If y is NaN, return false.
If x is the same Number value as y, return true.
If x is +0 and y is −0, return true.
If x is −0 and y is +0, return true.
Return false.
If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions); otherwise, return false.
If Type(x) is Boolean, return true if x and y are both true or both false; otherwise, return false.
Return true if x and y refer to the same object. Otherwise, return false.
And here's what it has to do for ==:
If Type(x) is the same as Type(y), then
If Type(x) is Undefined, return true.
If Type(x) is Null, return true.
If Type(x) is Number, then
If x is NaN, return false.
If y is NaN, return false.
If x is the same Number value as y, return true.
If x is +0 and y is −0, return true.
If x is −0 and y is +0, return true.
Return false.
If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions). Otherwise, return false.
If Type(x) is Boolean, return true if x and y are both true or both false. Otherwise, return false.
Return true if x and y refer to the same object. Otherwise, return false.
If x is null and y is undefined, return true.
If x is undefined and y is null, return true.
If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).
If Type(x) is String and Type(y) is Number,
return the result of the comparison ToNumber(x) == y.
If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
If Type(x) is either String or Number and Type(y) is Object,
return the result of the comparison x == ToPrimitive(y).
If Type(x) is Object and Type(y) is either String or Number,
return the result of the comparison ToPrimitive(x) == y.
Return false.
Notice that if Type(x) equals Type(y) then the operators do the same thing. However, if they aren't, then the == might have to do various conversions whereas === just returns false.
For the links you gave, the types that are being compared are actually the same, so the two operators should perform about equally. Differences here would be based on implementation details - since they do different things, they can be optimized for differently. Theoretically, since === does less, one would think it would always be faster, but that doesn't appear to be the case for certain builds of Firefox, at least if those benchmarks are accurate.
However, see the difference if the types are different. When doing "hi" === {} you get ~66 million ops/second, but for "hi" == {} you only have ~4 million ops/second.
JavaScript is a weakly typed language, so it will apply type coercion wherever possible.
Equals Operator
// These are true
new Number(10) == 10; // Number.toString() is converted
// back to a number
10 == '10'; // Strings gets converted to Number
10 == '+10 '; // More string madness
10 == '010'; // And more
isNaN(null) == false; // null converts to 0
// which of course is not NaN
The Strict Equality Operator
It works like the normal equality operator, except that strict equality operator does not perform type coercion between its operands.
"" === "0" // false
0 === "" // false
0 === "0" // false
false === "false" // false
false === "0" // false
false === undefined // false
false === null // false
null === undefined // false
" \t\r\n" === 0 // false
The above results are a lot clearer and allow for early breakage of code. This hardens code to a certain degree and also gives performance improvements in case the operands are of different types.
So === faster than == in Javascript
Here is good Reference
=== compares if the values and the types are the same.
== compares if the values are the same, but it also does type conversions in the comparison. Those type conversions make == slower than ===.

Empty array is false, but in IF statement it returns true [duplicate]

This question already has answers here:
Empty arrays seem to equal true and false at the same time
(10 answers)
Closed 9 years ago.
This returns true:
[] == false
But here, alert is called:
if([]){ alert('empty array is true here'); }
Can you explain why?
According to section 11.9.3 of the ECMAScript® Language Specification, any == comparison is done as follows:
If Type(x) is the same as Type(y), then
If Type(x) is Undefined, return true.
If Type(x) is Null, return true.
If Type(x) is Number, then
If x is NaN, return false.
If y is NaN, return false.
If x is the same Number value as y, return true.
If x is +0 and y is −0, return true.
If x is −0 and y is +0, return true.
Return false.
If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions). Otherwise, return false.
If Type(x) is Boolean, return true if x and y are both true or both false. Otherwise, return false.
Return true if x and y refer to the same object. Otherwise, return false.
If x is null and y is undefined, return true.
If x is undefined and y is null, return true.
If Type(x) is Number and Type(y) is String,
return the result of the comparison x == ToNumber(y).
If Type(x) is String and Type(y) is Number,
return the result of the comparison ToNumber(x) == y.
If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
If Type(x) is either String or Number and Type(y) is Object,
return the result of the comparison x == ToPrimitive(y).
If Type(x) is Object and Type(y) is either String or Number,
return the result of the comparison ToPrimitive(x) == y.
Return false.
In the first step, ToNumber() is applied to false and yields Number(0). In the second step, rule #9 applies ToPrimitive() to the empty array and yields "" which, cast to a numeric value, becomes Number(0) as well.
Additionally section 9.2 says this about using an object in an expression:
The abstract operation ToBoolean converts its argument to a value of type Boolean according to this table:
Undefined -> false
Null -> false
Boolean -> The result equals the input argument (no conversion).
Number -> The result is false if the argument is +0, −0, or NaN; otherwise the result is true.
String -> The result is false if the argument is the empty String (its length is zero); otherwise the result is true.
Object -> true
this is because == in JS forces conversion and if one type can be converted to another the return value is true and here because [] cant be changed or compared with bool it is false where as if([]) checks for null and undefined values and because [] is neither null or undefined it is returning true
check this
Which equals operator (== vs ===) should be used in JavaScript comparisons?

ECMA-262 === algorithm explanation

I was trying to understand the exact algorithm for the === operator in JavaScript. It is defined as something like
The comparison x === y, where x and y are values, produces true or false. Such a comparison is performed as follows:
If Type(x) is different from Type(y), return false.
If Type(x) is Undefined, return true.
If Type(x) is Null, return true.
If Type(x) is Number, then
If x is NaN, return false.
If y is NaN, return false.
If x is the same Number value as y, return true.
If x is +0 and y is −0, return true.
If x is −0 and y is +0, return true.
Return false.
If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions); otherwise, return false.
If Type(x) is Boolean, return true if x and y are both true or both false; otherwise, return false.
Return true if x and y refer to the same object. Otherwise, return false.
Now if I write something like
var t1 = undefined,t2 = 2;
typeof(t1); //"undefined"
typeof(t2); //"number"
t1 === t2; //returns false ?????
Consider point 2 and 3: It should return true instead.
I am testing it in Chrome 15.0.874.106 m. Can somebody explain what exactly is going on in this case?
You have to go in order, If Type(x) is different from Type(y), return false.. Since false is already returned, it never gets to point 2 or 3.
Considering 1: If Type(x) is different from Type(y), return false.,
t1 === t2 should indeed return false.
t1 is undefined, while t2 is a number.

Javascript: Explain If Type(x) is Undefined, return true. If Type(x) is Null, return true

In JavaScript spec: http://www.ecma-international.org/publications/standards/Ecma-262.htm
11.9.6 The Strict Equality Comparison Algorithm
The comparison x === y, where x and y are values, produces true or
false. Such a comparison is performed as follows:
If Type(x) is different from Type(y), return false.
If Type(x) is Undefined, return true.
If Type(x) is Null, return true.
If Type(x) is Number, then
If x is NaN, return false.
If y is NaN, return false.
If x is the same Number value as y, return true.
If x is +0 and y is -0, return true.
If x is -0 and y is +0, return true.
Return false.
If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in
corresponding positions); otherwise, return false.
If Type(x) is Boolean, return true if x and y are both true or both false; otherwise, return false.
Return true if x and y refer to the same object. Otherwise, return false. NOTE This algorithm differs from the SameValue Algorithm (9.12)
in its treatment of signed zeroes and NaNs
What does the bolded section mean? How do you write out some JavaScript to confirm it?
I tried alert(typeof(undefined) === 'x'); but it gives me false.
Before that it says:
where x and y are value
So first, give x and y values.
Then forget "blah", 1 is important. x and y have to be the same type to get past step 1.
Step 2 is "If Type(x) is Undefined, return true.".
There is only one value that gives the undefined type, undefined. Thus, the only way to test step 2 (short of assigning undefined to variables):
alert(undefined === undefined)
… will give true.
Step 3 works in exactly the same way. The only null value is null.
alert(null === null)
A manual implementation the algorithm would start like this:
function equalsequalsequals(x, y)
if (typeof x != typeof y) {
return false;
} else if (typeof x == "undefined") {
return true;
} // …
The typeof operator can't tell us if something is null or not, so you can't completely implement the algorithm without using ===. Since we have ===, however, we don't need to.
My interpretation is one of reading the spec as a formal proof, having the numbers 2 and 3 dependent on the first rule:
If Type(x) is different from Type(y), return false.
Since you are at step 2 and have not returned false, the type of X must be the same as of Y, so the condition it is checking for is:
(null) === (null)
which returns true. The same applies with Undefined
Type() is an internal method, not available in your javascript code. It is not identical to the typeof operator.
x and y refer to the left and right hand operands of ===. They are not strings 'x' and 'y'.
http://es5.github.com/#x11.9.6

Categories