0

I would like to know why this happens and what is the primitive value of "a"?

a = [1,2,3]
b = "1,2,3"
a==b //this returns true

As far as I know when doing this the primitive type of a is compared with b.

EDIT:

According to the section "Loose equality using ==" When you compare an object (a) with a string (b) the following rule is valid:

ToPrimitive(A) attempts to convert its object argument to a primitive value, by attempting to invoke varying sequences of A.toString and A.valueOf methods on A.

Undestanding how "ToPrimitive(A)" works was crucial.

Bruno Casarotti
  • 623
  • 8
  • 23
  • 2
    `==` specifically ignores the type of the values it compares. – JJJ Sep 20 '17 at 13:14
  • you should check this out https://www.destroyallsoftware.com/talks/wat, it won't answer your question and it'll give you a dozen more. – Miniver Cheevy Sep 20 '17 at 13:15
  • Ok but it fails when there is an object inside it – Bruno Casarotti Sep 20 '17 at 13:15
  • Generaly in js, using `===` is safer and less random than `==`. But check Miniver Cheevy's link – m.nachury Sep 20 '17 at 13:16
  • Use a===b. For the object, share example – Amit Kumar Singh Sep 20 '17 at 13:16
  • If it "fails when there is an object inside it" then the string representation of the array isn't equal to the string you're comparing with. – JJJ Sep 20 '17 at 13:16
  • Objects are unique. { "key" : 1 } == { "key" : 1 } is false. If you want to comapre simple objects, you can JSON.stringify() them and comapre the string. For complex objects, you need a loop. – Shilly Sep 20 '17 at 13:17
  • == does type conversion so its being converted to a string, which is "1,2,3"...I think. why the downvotes, this is a legit question. – terpinmd Sep 20 '17 at 13:17
  • Possible duplicate of [Which equals operator (== vs ===) should be used in JavaScript comparisons?](https://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons) – Ivar Sep 20 '17 at 13:18
  • Didn't downvote, but these are trivial js things you learn early in tutorials, so ppl downvote because it shows not enough effort. – Shilly Sep 20 '17 at 13:18
  • I really checked into it, going to primitive types and all...People are kind of mad here. – Bruno Casarotti Sep 20 '17 at 13:19
  • Always look on MDN and such first, it's the best reference imho. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators Maybe the tutorials you use are bad if they didn't explain the diff in the first chapters. Have fun learning. :) – Shilly Sep 20 '17 at 13:24

1 Answers1

10

It returns true because you are not using the strict comparison operator ===.

When using == you are actually doing [1,2,3].toString() === '1,2,3'

Equality (==) (source)

The equality operator converts the operands if they are not of the same type, then applies strict comparison. If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory.

Identity / strict equality (===) (source)

The identity operator returns true if the operands are strictly equal (see above) with no type conversion.

console.log('[1,2,3] == "1,2,3"', [1,2,3] == "1,2,3");

console.log('[1,2,3] === "1,2,3"', [1,2,3] === "1,2,3");

console.log('[1,2,3].toString() === "1,2,3"', [1,2,3].toString() === "1,2,3");

The Abstract Equality Comparison Algorithm (source)

The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:

1.If Type(x) is the same as Type(y), then

____a. If Type(x) is Undefined, return true.

____b. If Type(x) is Null, return true.

____c. If Type(x) is Number, then

________i If x is NaN, return false.

________ii If y is NaN, return false.

________iii If x is the same Number value as y, return true.

________iv If x is +0 and y is −0, return true.

________v If x is −0 and y is +0, return true.

________vi Return false.

____d. If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions). Otherwise, return false.

____e. If Type(x) is Boolean, return true if x and y are both true or both false. Otherwise, return false.

____f Return true if x and y refer to the same object. Otherwise, return false.

2. If x is null and y is undefined, return true.

3. If x is undefined and y is null, return true.

4. If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).

5. If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.

6. If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.

7. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).

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

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

10. Return false.

codtex
  • 6,128
  • 2
  • 17
  • 34
  • So any type that I compare not using the strict operator is going to convert the values to string? – Bruno Casarotti Sep 20 '17 at 13:18
  • 1
    When using it for primitive types, generally yes :) (_The equality operator converts the operands if they are not of the same type, then applies strict comparison._) – codtex Sep 20 '17 at 13:21
  • 2
    you may add a link to the table: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness – Nina Scholz Sep 20 '17 at 13:22
  • References added :). Thank you all – codtex Sep 20 '17 at 13:23
  • @NinaScholz The table in the "Loose equality using ==" is just what I was looking for – Bruno Casarotti Sep 20 '17 at 13:26
  • Your answer does not match the [rules here](http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3). I think it very misleading/wrong to be saying that different types are converted to string and then compared – musefan Sep 20 '17 at 13:30
  • @musefan this link was the origin of my question hahaha. The table that NinaScholz posted solved it – Bruno Casarotti Sep 20 '17 at 13:37