js-two empty arrays why not equal?

Source: Internet
Author: User

var a = [], B = [];console.log (a==b);

What is the result of the console printing? The answer is: false.

Next look at the parsing:

The comparison of the original values is the comparison of the values:

They are equal when their values are equal (= =)

They are identical when their values and types are equal (= = =).

Unlike the original value, the object's comparison is not a comparison of values, but rather a comparison of references:

Even if two objects contain the same attributes and the same values, they are not equal

Even if the index elements of each of the two arrays are exactly equal, they are not equal

Give me a chestnut:

var o = {x:1}, p = {x:1};      // two objects with the same property o = = P                        //  + = false: Two separate objects never equal var a = [], b = [];            // two separate empty arrays a = = B                        // = = false: Two separate arrays are never equal

We usually refer to the object as a reference type (reference type), which distinguishes it from the basic type of JavaScript. Object values are references (reference), and comparisons of objects are reference comparisons: they are equal when and only if they reference the same base object.

Give me a chestnut:

var a = [];  // defines a variable A that references an empty array var b = A;   // variable B refers to the same array b[0] = 1;    // Modify the referenced array with variable b a[0]         // + 1: Variable A also modifies a = = = B      //  = = True : A and B refer to the same array, so they are equal

js-two empty arrays why not equal?

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.