Skip to content

Instantly share code, notes, and snippets.

@roine
Created December 10, 2012 08:49
Show Gist options
  • Save roine/4249395 to your computer and use it in GitHub Desktop.
Save roine/4249395 to your computer and use it in GitHub Desktop.
//compare two distinct array
// check for length and value at each index
function equalArrays(a,b) {
if (a.length != b.length) return false;
for(var i = 0; i < a.length; i++)
if (a[i] !== b[i]) return false;
return true;
}
var a = ['hello', 'world'],
b = ['hello', 'world'];
a === b;
/* return false */
equalArrays(a, b);
/* return true */
var a = [1];
var b = a;
a == b;
a === b;
/* return true because they refere to the same value */
a[0] = 2;
a[0]; /* 2 */
b[0]; /* 2 */
/* here it's obvious that a and b share the same reference */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment