The role of JavaScript's three equals sign (= = =)

Source: Internet
Author: User

JavaScript's three equals sign (= = =) Function:
In code, it is often necessary to compare two operands for equality, JavaScript provides two comparison operators, one is the most frequent common equality operator (= =), and the other is to use the strict equality operator (= = =), which is not as frequent as the first one, but is more restrictive.
One. Ordinary equals operator:
The comparison rules for this operator are relatively loose because the two operands are data type conversions before the comparison is made.
The rules are as follows:
1. If there is a Boolean value in the operand, first convert this boolean value to a numeric type. True to convert 1,false to 0.
2. If one of the operands is a number and one is a string, then the string is attempted to be converted to a number before the comparison.
3. If one of the operands is a string and one is an object, then the object is attempted to be converted to a string before being compared.
4. If one of the operands is a number and one is an object, then the object will be converted to a number before the comparison.
code example:

true==1; // true10==10; // true"10" ==10; // true var a={x:10}var b={x:10}a.x==b.x; // true

As can be seen from the above code, because the data type conversion is performed, the comparison rules are relatively loose, and the string can be equal to the numeric type.
Two. Congruent operators:
The operator comparison rules are strict in this, mainly because the two operands do not convert data types before the comparison.
code example:

true===1; // false10===10; // true"10" ===10; // false var a={x:10}var b={x:10}a.x===b.x; // true

The only difference between the above code and the first code instance is to replace the normal operator with the equality operator, but the result is very different.

The analysis is as follows:

1.true===1;//false, the nature is not equal because no type conversions are performed.

2.10===10;//true,10 is equal to 10, this does not have to say much.

3. "Ten" ===10;//false, the string "10" is naturally not equal to the value 10, since no type conversion is performed.

4.a.x===b.x;//true, because the value of a.x is 10,b.x value is also 10, they are naturally equal.

There is no problem with the above understanding, then look at a code example:

var a={x:10}var b={x:10}c=a;c===a//true; A===b//flase

In the above code, since C and a both point to the same object, so is congruent, A and B are not two points to the same object, so they are congruent with each other.

Special Note: Two objects compare a reference to a two object, if all point to the same object.

Some special cases of the two operators:

1.null and undifined:

NULL // true NULL // false

2. If one of the operands is Nan, it is not equal:

Nan===nan//Nan==nan//false

The original address is: http://www.51texiao.cn/javascriptjiaocheng/2015/0430/486.html

The original address is: http://www.softwhy.com/

The role of JavaScript's three equals sign (= = =)

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.