0

Some unit tests are failing. Upon debugging, I traced the problem here

var a = "USD 1,234.12"
var b = "USD 1,234.12"
console.log(a === b)

String a has been generated by a currency formatter library and String b has been written by a unit test developer.

I don't understand why these two strings that look the same aren't considered equal by ===. What is happening here?

enter image description here

ggorlen
  • 44,755
  • 7
  • 76
  • 106
TSR
  • 17,242
  • 27
  • 93
  • 197
  • 1
    Two strings are only equal if they have the same amount of code units, and, for each index, the code unit of one string matches the code unit of the other string. This level of debugging is missing here. You could have done `Array.from(a).findIndex((char, index) => char !== b[index])`. You could have done `Array.from(a).forEach((char, index) => console.log("a", char, char.codePointAt(), "vs. b", b[index], b[index].codePointAt()))`, or [some other approach](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#instance_methods). – Sebastian Simon Apr 11 '21 at 17:14
  • 3
    Bad close reason. It's very reproducible (click the run snippet button), it's interesting (if you don't know about different kind of spaces) and has a clear answer. – Evert Apr 11 '21 at 17:24
  • @Evert this is also half of a debug process, a better question should be "how to do equality tests without considering the different types of spaces in a string" – Mister Jojo Apr 11 '21 at 17:37
  • @Evert actually, after understanding the problem, how to answer this "how to do equality tests without considering the different types of spaces in a string" – TSR Apr 11 '21 at 17:39
  • @TSR I do this post for you : https://stackoverflow.com/questions/17973964/how-to-compare-two-strings-in-java-without-considering-spaces – Mister Jojo Apr 11 '21 at 18:28
  • These sort of "trick" (accidental or intentional) questions with zero-width characters are well-known: https://meta.stackoverflow.com/questions/351807/consider-displaying-zero-width-space-characters-in-code-blocks – ggorlen Apr 11 '21 at 18:46
  • @ggorlen String a has been generated by a Currency Formatter library and String b has been writing by a unit test developer. I don't see any "trick" in this question. It is totally legitimate – TSR Apr 11 '21 at 18:54
  • 1
    @TSR If that's the scenario, please edit the post to state that. This question has been asked before in various forms, but I don't see an obvious dupe target and this looks like the cleanest reproduction, so I propose it be canonical. Upvoted. – ggorlen Apr 11 '21 at 19:43

1 Answers1

10

There's a hidden difference in your two strings. Run this:

var a = "USD 1,234.12"
var b = "USD 1,234.12"

for (var i = 0; i < a.length; i++) {
  console.log(a.codePointAt(i), b.codePointAt(i));
}

The space in the b string is a regular space (32), while the space in the a string is a Unicode non-breaking space (160).

ggorlen
  • 44,755
  • 7
  • 76
  • 106
Evert
  • 93,428
  • 18
  • 118
  • 189