Skip to main content
2 of 3
"stringify" is the standard in javascript, not "encode"
nategood
  • 12k
  • 4
  • 38
  • 45

Comparing two arrays in Javascript

I'd like to compare two arrays... ideally, efficiently. Nothing fancy, just true if they are identical, and false if not. Not surprisingly, the comparison operator doesn't seem to work.

var a1 = [1,2,3]; var a2 = [1,2,3]; console.log(a1==a2); // Returns false console.log(JSON.stringify(a1)==JSON.stringify(a2)); // Returns true 

JSON encoding each array does, but is there a faster or "better" way to simply compare arrays without having to iterate through each value?

Julian H. Lam
  • 26.3k
  • 13
  • 51
  • 74