JavaScript equality vs. comparison

Source: Internet
Author: User
Tags object comparison string

Equality and comparison
JavaScript has two ways of determining whether two values are equal.
equals operator
The equals operator consists of two equals: = =

JavaScript is a weakly typed language, which means that the equals operator enforces type conversions to compare two values.

"" = = "0"//False
0 = ""//True
0 = "0"//True
False = = "false"//False
false = = "0"//True
false = = undefined//False
false = = NULL//False
NULL = = undefined//True
"\t\r\n" = 0//True

The table above shows strongly typed conversions, which is the main reason why using = = is widely regarded as a bad programming habit.
Because of its complex conversion rules, it can cause problems that are difficult to track.

In addition, coercion of type conversions can also result in performance consumption, such as a string that, in order to be compared to an array, must be cast to a number beforehand.
Strictly equals operator
Strictly equal to the operator consists of three equals: = = =
Do not want the normal equals operator, strictly equal to the operator will not be forced type conversion.

"" = = = "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 results above are clearer and helpful for code analysis. If the two operand types are different, it is certainly not equal and can also contribute to performance improvement.
Compare objects
Although the = = and = = operators are equal to operators, the behavior is different when one of the operands is an object.
{} === {}; False
New String (' foo ') = = ' Foo '; False
New Number (10) = = 10; False
var foo = {};
Foo = = = = Foo; True
Here equals the operator comparing whether the value is equal, but whether it belongs to the same identity, that is to say, only the same instance of the object is considered equal.
It's kind of like a pointer comparison in the IS and C in Python.
Conclusion
It is strongly recommended to use strict equals operator. If the type requires conversion, the explicit conversion should precede the comparison.
Rather than using the complex cast rules of the language itself.



Related Article

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.