JavaScript advanced series-equality and comparison of Types

Source: Internet
Author: User
The equals operator is composed of two equal signs: JavaScript is a weak type language, which means that the equals operator performs forced type conversion to compare two values.
  • Equal to operator

  • Strictly equal to operator

  • Comparison object

  • Conclusion


JavaScript can be used to determine whether two values are equal.

Equal to operator

The equals operator is composed of two equal signs: =

JavaScript is a weak type language, which means that equal operators perform forced type conversion to compare two values.

""           ==   "0"           // false0            ==   ""            // true0            ==   "0"           // truefalse        ==   "false"       // falsefalse        ==   "0"           // truefalse        ==   undefined     // falsefalse        ==   null          // falsenull         ==   undefined     // true" \t\r\n"    ==   0             // true

The above table shows forced type conversion, which is also the main cause of the use of = is widely considered to be a bad programming habit, because of its complex conversion rules, it will lead to difficult to trace problems.

In addition, forced type conversion also results in performance consumption. For example, to compare a string with a number, it must be forcibly converted to a number in advance.


Strictly equal to operator

The strictly equal operator is composed of three equal signs: =

Unlike normal equals operators, strict equals operators do not perform forced type conversion.

""           ===   "0"           // false0            ===   ""            // false0            ===   "0"           // falsefalse        ===   "false"       // falsefalse        ===   "0"           // falsefalse        ===   undefined     // falsefalse        ===   null          // falsenull         ===   undefined     // false" \t\r\n"    ===   0             // false

The above results are clearer and helpful for code analysis. If the two operands have different types, they are definitely not equal and will also help improve performance.


Comparison object

Although the = and = operators are all equal to operators, when one of the operands is an object, the behavior is different.

{} === {};                   // falsenew String('foo') === 'foo'; // falsenew Number(10) === 10;       // falsevar foo = {};foo === foo;                 // true

Here, the operator compares not whether the values are equal, but whether they belong to the same identity. That is, only the same instance of the object is considered equal. This is a bit like a pointer in Python and C.

Note: For a more intuitive view of the difference between = and =, see JavaScript Equality Table


Conclusion

Strictly equal operators are strongly recommended. If the type needs to be converted, Explicit conversions should be performed before comparison, instead of using the complicated forced conversion rules of the language itself.

The above is the content in the JavaScript advanced series-equal and comparative content in the type. For more information, see PHP Chinese Network (www.php1.cn )!

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.