Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news1.tnib.de!feed.news.tnib.de!news.tnib.de!news.belwue.de!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <1934880.aqSPkdTlGX@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Thu, 26 Jan 2012 19:50:57 +0100 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 8Bit Subject: Re: Comparing arraies Newsgroups: comp.lang.javascript References: <8dc6b834-cd20-47a5-a085-f4d5e4402c38@c13g2000yqk.googlegroups.com> <4f218d62$0$6578$9b4e6d93@newsspool3.arcor-online.net> Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 73 NNTP-Posting-Date: 26 Jan 2012 19:51:00 CET NNTP-Posting-Host: 099843c6.newsspool3.arcor-online.net X-Trace: DXC=:<^_INOOMQ4lIh70@IFKdLPRg34@@;FjVm77< X-Complaints-To: usenet-abuse@arcor.de Xref: x330-a1.tempe.blueboxinc.net comp.lang.javascript:10607 Martin Honnen wrote: > Archos wrote: >> How to know if 2 arraies are equal? I was expecting that it could be >> checked so: >>>>> var array1 = [1,2,3,4], array2 = [1,2,3,4]; >>>>> if (array1 === array2) { console.log("array1 and array2 are equal"); } >> >> Since the comparing of strings or integers works: >> >> var a = "2", b = "2"; >> if (a === b) { console.log("a == b"); } > > No, for objects (and arrays are objects) the identity is checked, no > comparison of properties or elements is done. > So with e.g. > > var a1, a2; > > a1 = a2 = [1, 2, 3, 4]; > > a1 == a2 > > you get true as the two variables hold references to the same array > object. Correct. That example might be confusing, though. > If you want to compare the elements you will need to define and > implement your own comparison function I think Not necessarily. One way without a custom comparison function that works for *simple* cases like this is to compare the string representations of the Array instances, which are (without overwriting the toString() method) the comma-separated string representations of the corresponding array's elements: /* "1,2,3,4" == "1,2,3,4" */ a1.toString() == a2.toString() or String(a1) == String(a2) or ("" + a1) == ("" + a2) That is, those two arrays are considered equal if the string representations of their elements are the same and occur in the same order (if we assume that both instances use the same toString() method). The caveat is that the string representations of elements can be the same, although the elements are different. Consider this: var o = {foo: 'bar'}; var o2 = {bar: 'baz'}; /* "[object Object]" == "[object Object]" */ [o].toString() == [o2].toString() Instead of the `==' operator, the `===' operator may be used as well, but it is neither supposed to add efficiency nor would it add expressive power (both arguments are of the same type), while it certainly reduces the backwards-compatibility of the entire code. PointedEars -- > If you get a bunch of authors […] that state the same "best practices" > in any programming language, then you can bet who is wrong or right... Not with javascript. Nonsense propagates like wildfire in this field. -- Richard Cornford, comp.lang.javascript, 2011-11-14