-1
let i = [5, 6, 7];
let j = [5, 6, 7];
console.assert(i === j, "Not the same values");

Will throw an error even if arrays contain the same values, how do you do a quick console.assert() to check if two arrays contain the same values?

Leigh Mathieson
  • 1,658
  • 2
  • 17
  • 25
  • 3
    Possible duplicate of [How to compare arrays in JavaScript?](https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript) – JJJ Apr 06 '18 at 15:37
  • 1
    `==` _also_ checks for reference. The difference between `==` and `===` is **type coercion**, not reference comparison or deep comparison or anything like that. – Sebastian Simon Apr 06 '18 at 15:42
  • Thanks, I put two questions in one there in error and can see 1 of them was likely to be a duplicate. There wasn't a question relating to console.assert() specifically and how to check. I was trying to help anyone searching for that, as I had to do the .toString() comparison check, so I hope it still helps some people like me to find it quickly – Leigh Mathieson Apr 06 '18 at 15:51

1 Answers1

-2
console.assert(i.toString() === j.toString(), "Doesn't match");

Will assert true, and is one way to compare values in two arrays using console.assert();

Leigh Mathieson
  • 1,658
  • 2
  • 17
  • 25
  • There are many more or less edge cases where this will give a wrong result. – JJJ Apr 06 '18 at 15:46
  • … or even fail completely. There are far better and safer ways to do this. – Sebastian Simon Apr 06 '18 at 15:47
  • Thanks, if you could please provide safe way to achieve the same – Leigh Mathieson Apr 06 '18 at 15:57
  • [How to compare arrays in JavaScript](https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript) – JJJ Apr 06 '18 at 16:01
  • Thanks, it was really to help anyone specifically searching for solutions on comparing arrays via console.assert() as there aren't any solutions when you search anywhere on S/O. That was what I was searching for and no answers, so hoped it would save some time for some people - it seems to be being down-voted as a question although I am fairly sure it's unique, but let's close it if we think asked previously, I hope it may help some people who search for it – Leigh Mathieson Apr 06 '18 at 16:22