Do you really understand the = = equals operator in JavaScript?

Source: Internet
Author: User

var i = 2;
Number.prototype.valueOf = function () {
return i++;
};
var a = new number (42);
if (a = = 2 && A = = 3) {
Console.log ("Yep, this happened.");

}

============================


"0" = = NULL; False
"0" = = undefined; False
"0" = = false; True--UH oh!
"0" = = NaN; False
"0" = = 0; True
"0" = = ""; False


false = = NULL; False
false = = undefined; False
false = = NaN; False
false = = 0; True--UH oh!
false = = ""; True--UH oh!
false = = []; True--UH oh!
False = = {}; False


"" = = null; False
"= = undefined; False
"" = = NaN; False
"= = 0; True--UH oh!
"" == []; True--UH oh!
"" == {}; False


0 = = NULL; False
0 = = undefined; False
0 = = NaN; False
0 = = []; True--UH oh!
0 = = {}; False



[] == ! []; True

2 = = [2]; True
"" = = [NULL]; True

0 = = "\ n"; True


var a = null;
var B = Object (a); Same as ' Object () '
A = = B; False
var c = undefined;
var d = Object (c); Same as ' Object () '
c = = D; False
var e = NaN;
var f = Object (e); Same as ' new number (e) '
E = = f; False


======================================================

Explanation in the ES5 specification:

Comparing:objects to Nonobjects
If an object/function/array are compared to a simple scalar primitive
(string, number, or Boolean), the ES5 spec says in clauses
11.9.3.8-9:
1. If Type (x) is either a String or number and Type (y) is Object,
Return the result of the comparison x = = Toprimitive (y).
2. If Type (x) is an Object and Type (y) is either String or number,
Return the result of the comparison toprimitive (x) = = Y.



Comparing:nulls to Undefineds
Another example of implicit coercion can be seen with = = Loose
Equality between null and undefined values. Yet again quoting the
ES5 spec, clauses 11.9.3.2-3:
1. If x is null and y is undefined, return true.
2. If x is undefined and y is null, return True.



Comparing:anything to Boolean
1. If Type (x) is Boolean, return the result of the comparison
Tonumber (x) = = Y.
2. If Type (y) is a Boolean, return the result of the comparison x = =
Tonumber (y).


Comparing:strings to Numbers

1. If Type (x) is number and Type (y) is String, return the result of
The comparison x = = Tonumber (y).
2. If Type (x) is a String and Type (y) is number, return the result of
The comparison tonumber (x) = = Y.


============================== Intuitive Comparison Chart

http://dorey.github.io/JavaScript-Equality-Table/

Do you really understand the = = equals operator in JavaScript?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.