Javascript string comparison fails when comparing unicode characters

后端 未结 4 1311
温柔的废话
温柔的废话 2020-11-30 08:18

I want to compare two strings in JavaScript that are the same, and yet the equality operator == returns false. One string contains a special character (eg. the

4条回答
  •  离开以前
    2020-11-30 08:41

    The JavaScript equality operator == will appear to be failing under the following circumstances. In all cases it is programmer error. Not a bug in JavaScript.

    1. The two strings do not contain the same number and sequence of characters.

    2. There is whitespace or newlines before, within or after one string. Use a trim() operator on both and look closely at both strings.

    3. Surprise typecasting. The programmer is comparing datatypes that are incompatible.

    4. There are unicode characters which look identical to other unicode characters but in fact are different unicode characters.

提交回复
热议问题