Why are arrays equal to their corresponding strings?

你。 提交于 2020-06-23 03:33:23

问题


Why is an array evaluated to true when it is compared to its corresponding string?

var a = [1,2,3];
var b = '1,2,3';

console.log(a==b);// true

Variable a stores the memory address of the array it is assigned. Then how is a memory address equal to corresponding string of that array.


回答1:


Well in case of "==", array is converted to toString and then compared due to loose comparison, so it equates to true. So what happens is this:

  var a = [1,2,3];
  var b = '1,2,3';

  a == b //is same as
  a.toString() === b //true

If you want to evaluate to true in a strict mode, you may also do something like this:

var a = [1,2,3];
var b = '1,2,3';

a = a.join(',')

console.log(b === a);

Whenever you're loose comparing (with '=='), javascript interpreter tries it best to to convert both values to a common type and match them. To quote MDN

Loose equality compares two values for equality, after converting both values to a common type. After conversions (one or both sides may undergo conversions), the final equality comparison is performed exactly as === performs it.

Now your confusion is,

what variable a stores inside it , is just memory address of the array . So in first code snippet you say that a==b is same as a.toString==b but whats inside a is a memory address ,so when a memory address is converted to a string how is it equal to corresponding strings of an array.

Well, note here when you comparing two variables, you're not comparing the memory address of them but the values stored at them :) So it's not the memory address that is getting converted to toString but the value stored at it.

Also, there's one more flaw in this thinking.

consider,

        var a = 4, // a holds address of variable a
        b =4; //b holds the address of variable b

Now, these two variables are definitely holding different memory addresses so they wouldn't have been equated to true which is not true. I hope you got the point.




回答2:


== compares two variables irrespective of data type while === compares two variables in a strict check, which means it checks for data type also then it returns true or false.

The ‘==’ operator tests for abstract equality i.e. it does the necessary type conversions before doing the equality comparison.

But the ‘===’ operator tests for strict equality i.e it will not do the type conversion hence if the two values are not of the same type, when compared, it will return false.




回答3:


In JS you have two Comparison Operators:

  • == Equal to

  • === Equal value and equal type

var a = [1, 2, 3];
var b = '1, 2, 3';

console.log(a == b);  // Outputs true
console.log(a === b);  //Outputs false



回答4:


It is due to the specs and semantics of the language. The array, which is an Object, will be coerced to a string before comparison to a string.

You can follow the conversion path:
7.2.15 Abstract Equality Comparison

  1. If Type(x) is Object and Type(y) is either String, Number, BigInt, or Symbol, return the result of the comparison ? ToPrimitive(x) == y.

It basically passes through all the steps until it gets to 11..
ToPrimitive(Array) eventually ends up referencing and calling the property .toString https://tc39.es/ecma262/#sec-array.prototype.tostring
https://tc39.es/ecma262/#sec-object.prototype.tostring



来源:https://stackoverflow.com/questions/62207802/why-are-arrays-equal-to-their-corresponding-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!