0

For example:

var one = ['H', 'i'];
var two = ['H', 'i'];

(one == two) returns false

but

(one.join('') == two.join('')) returns true

Why is that?

3
  • 4
    stackoverflow.com/questions/3115982/… Commented Feb 16, 2014 at 9:45
  • 2
    one and two are different objects that contain the same data. Commented Feb 16, 2014 at 9:45
  • @AustinBrunkhorst I still don't understand why what I was doing doesn't work. The stack post you posted proposes a solution to how to compare two character arrays, but that's not what I was asking. Commented Feb 16, 2014 at 9:50

2 Answers 2

3

There is a difference on how equality is defined for strings and arrays - strings are considered equal if their contents are identical, but arrays are considered equal only if it's the same array, and different otherwise even if their contents match.

There are a bunch of reasons why it could be the way it is, for example two reasons:

1) you often don't want array comparison to go through the whole array, because it could be huge and would take a huge time. So the default way shouldn't be dangerous.

2) you can alter array contents while still being 'the same' array; while javascript strings are immutable so any changed string is a new, different object.

Sign up to request clarification or add additional context in comments.

Comments

1

When comparing objects, JS wants to see if they are the actual same object, not just an object with the same contents.

I find underscore's isEqual method useful here, but if you want to figure out how it is done library free, just glance at underscores core, which is very easy to read

http://underscorejs.org/#isEqual

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.