Triple equal signs return false for arrays in javascript. why?

后端 未结 4 1644
南方客
南方客 2020-11-27 19:15

I know that === is typically referred to as the identity operator. Values being compared must be of the same type and value to be considered equal. Then why bel

相关标签:
4条回答
  • 2020-11-27 19:38

    because if it's not a primitive type (String, Number, Boolean), if it's an array or an object then the comparison operators will check if it's exactly the same instance. So

    var a = ['as','ds'];
    var b = a;
    var c = ['as','ds'];
    b == a; //will return true; (doesn't matter if it's == or === for non primitive types)
    b == c; //will return false;
    

    so basically you need to define your own method to compare arrays and see if all elements are the same. This sort of function usually doesn't exist because it can be very expensive and there's usually another way to write that sort of thing.

    0 讨论(0)
  • 2020-11-27 19:39

    While array1==array2 doesn't seem to work (object identity equality),

    Easiest option, works in almost all cases, except that null!==undefined but they both be converted to null and be considered equal:

    function arraysEqual(a1,a2) {
        return JSON.stringify(a1)==JSON.stringify(a2);
    }
    
    0 讨论(0)
  • 2020-11-27 19:47

    First of all === is strict equality, not an identity operator, and Arrays, like objects are reference objects, not value objects as in the case of numbers and strings...

    So when you are comparing those 2 arrays, you are creating two different arrays in memory. It'd be the same as saying...

    var x = { a: 1 }; 
    var y = { a: 1 };
    
    x === y; //false  
    

    You can't directly compare arrays or objects like that.

    0 讨论(0)
  • 2020-11-27 19:52

    They are not equal because a new array is being created in each of these statements, each being a brand new array object with just identical contents. If you create two new objects:

    var a = {};
    var b = {};
    
    a === b // false
    

    When you create new objects, arrays, functions, etc., a brand new object is placed into memory. Creating a new object with the same internals as another object will not magically cause that object to point to one that already exists. The objects may look the same, but they do not point to the same instance. Now if your statement had been like so:

    var arr = ['asdf'];
    
    arr === arr; // true
    

    This is obviously be true. === is strict equality, not an identity operator. When objects are ran through a strict equality operator, they are checked to see if they point to the same reference. As I explained earlier, each time you use new Array or [] that a brand new object will be created, each being a new and different reference. So there is no way that two arrays, or any object, can come out === being true unless they point to the exact same array. Just because two objects are being created with identical contents does not mean that they point to the same object, just two identical, but different objects.

    Think of constructing functions:

    var Car = function (color) {
        this.color = color;
    };
    
    var ford = new Car('green');
    var chevy = new Car('green');
    var toyota = ford;
    
    ford === chevy // false
    

    Just because you are using the same constructor does not mean that every time you call it the same object gets returned. Rather, a new object will be returned every time. Just because both cars are green doesn't mean it's the same car.

    ford === toyota // true
    

    This is now true because both variables point to the exact same Car reference.

    0 讨论(0)
提交回复
热议问题