The difference between = = and = = = in JS

Source: Internet
Author: User

= = and = = Definition

==and === Although they are all relational expression operators, they are defined differently:

    • ==: Called the equality Operator (equality Operator), which is used to detect the equality of two operations, where the definition of equality is very loose, allowing the conversion of a type
    • ===: called the strict equality operator (Strict Equality), also known as the identity operator (identity Operator) or the congruent operator, which is used to detect whether two operands are strictly equal
= = and = = = Arithmetic rules

In JavaScript == and === operators, their operations have their own arithmetic rules.

= = Arithmetic Rule

==Operator is not strict for two numbers. If the two operands are not of the same type, the equality operator attempts some type conversions and then compares them.

When you convert different data types, they follow the following basic principles:

If the two operands are of the same type, the comparison will be equal if they are strictly equal, as are strict equality rules. If they are not strictly equal, the comparison results are unequal.

If the types of the two operands are different, the == equality operators may also consider them equal. Detection of equality will be governed by the following rules and type conversions:

If one value is null and the other is undefined , then they are equal:

null == undefined; // => true

If one value is a number and the other is a string, the string is first converted to a numeric value and then compared using the converted value:

1 == ‘1‘; // => true

If one of the values is, it is true converted to a 1 second comparison. If one of the values is false , convert it to a 0 second comparison:

true = =0;=Falsefalse = =1;=Falsetrue = = 1; //= true false = = 0; //= true true = =  ' 1 '; //= true false =  ' 0 '; //= true        

If one value is an object and the other value is a number or string, the object is first used toString() and valueOf() converted to the original value before being compared.

Two operands follow these rules when comparing:

    • nullAnd undefined is equal to
    • Before you can compare equality, you cannot null undefined convert and transform to any other value
    • If there is an action NaN , the equality operator returns false , and the inequality operator returns true . Important: Even if the two operands are all NaN , the equality operator also returns false because, by rule, NaN not equal toNaN
    • If the two operands are objects, then the comparison is not the same object. If all two operands point to the same object, the equality operator returns true , otherwise returnsfalse

Consider the comparison of objects:

var a = [1,2,3];

var b = [1,2,3];

var c = {x: 1, y: 2};

var d = {x: < Span class= "number" >1, y: 2};

var e = Span class= "string" >

var f = < Span class= "string", "TE" +

A = = B //=>false c = = d //=>false E = = f //=>true

= = = Arithmetic Rule

The strict equality operator === first computes the value of its operand, and then compares the two values, and the comparison process does not have any type conversions. Its arithmetic rules follow the following rules:

If two value types are different, they are not equal

true === ‘1‘; // => false

Where the operand true is a Boolean value and ‘1‘ a string value.

If all two are or are null undefined , then they are equal:

null === null; // => trueundefined === undefined; // => truenull === undefined; // =>false

If two values are Boolean true or false , they are equal:

true = = =True=Truefalse = = =False=Truetrue = = = 1; //=>false true = = =  ' 1 '; //=>false false = = = 0; //=>false false = = =  ' 0 '; //=>false     

If one of the values is NaN , or if two values are NaN , they are not equal. NaNand any other value are not equal, including itself. The x !== x value of x NaN This expression is only for x NaN when true it is done.

If two values are numeric and the values are equal, they are equal. If one value is 0 and the other value is -0 , they are equally equal.

Reprinted from Http://www.w3cplus.com/javascript/which-equals-operator-vs-should-be-used-in-javascript-comparisons.html

The difference between = = and = = = in JS

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.