& Quot ;==& quot; or & quot ;==& quot; in JavaScript;

Source: Internet
Author: User

In most cases, we should use =. We can use x = null only when detecting null/undefined, because we usually do not distinguish null from undefined, x = null is abbreviated as x = null | x = undefined.

The = comparison seems convenient, for example, 1 = '1', but it will bury hidden risks, such as the assumption that the type may be incorrect.

Example:
If (x = 10) x + = 5 www.2cto.com

If the input x is a string '10', the result of x is '123 '. In subsequent operations, the string '123' may be transformed to introduce hidden errors.

 

In addition, programmers may unconsciously rely on certain assumptions-for example, equal comparisons should be passed, that is, a = B, B = c, and a = c. However, the = of JavaScript is not passed. For example, 0 = '0', 0 = '', but '0 '! = ''.

Therefore, the = of JS is not the equals () of Java/C #, because the Java/C # language requires that equals () must be passed.

In short, for large-scale programming, the small convenience of = cannot be compared with the risk.

 

Suggestion:

Forget =, only use =. = A design error. It will try to perform type conversion (not java equal, it is well defined), The type conversion rules are not easy to remember. It also destroys the transmission.
Only === ,! =, You will feel a little comfortable writing javascript

Author: QQ101209442
 

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.