JavaScript =, = =, = = = Difference detailed

Source: Internet
Author: User

JavaScript =, = =, = = = Difference detailed
Today found in project development in a small problem. When judging n== "" Result when n=0 n== "" The result also returns TRUE. Although it is a small problem, but has a big impact, so decided to go into the analysis.
1, = assignment operator

// Example: var n=1; Console.log (n); // 1n=n+1; Console.log (n); // 2

2. = = value comparison operator
The value comparison operator implicitly converts to the same data type when the data types are inconsistent on either side of the expression, and then compares the values.

// Example: var a= "1"; var b=1; Console.log (a==b); // true // Example: var a= ""; var b=0; Console.log (a==b); // true;

Note: because = = only compares values and does not perform data type comparisons, when the expressions on both sides are inconsistent, they are implicitly converted to the same data type and then compared,
So
0 is the logical false
1 is the logical true
An empty string is a logical false
var a= "";//Parse to False
var b=0;//parsing to False
So a==b False=false result true

3, = = = value type comparison operator
= = = The expression does not only compare values, but also to compare data types. Type conversions are not performed when the types are different, and in addition to comparing values, the data types on both sides are compared, which is the identity operator.

 //   a= "1" ;     var  b=1; Console.log (a  ===b); //    false  //        var  a= " var  b=0; Console.log (a  ===b); //    false;  Console.log (false  ===0); //    false;  Console.log ("1" = = = 1); // false;  

4, = = and = = = Difference
4.1 There is a difference between = = and = = = When the same data type is compared.
4.2 Comparison between different types, = = will convert both sides of the expression to the same type after the comparison is equal, = = = If the type is different, do not convert, the result is unequal.

JavaScript =, = =, = = = Difference detailed

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.