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 danish å
).
JavaScript code:
var filenameFromJS = "Designhåndbog.pdf";
var filenameFromServer = "Designhåndbog.pdf";
print(filenameFromJS == filenameFromServer); // This prints false why?
The solution What worked for me is unicode normalization as slevithan pointed out.
I forked my original jsfiddle to make a version using the normalization lib suggested by slevithan. Link: http://jsfiddle.net/GWZ8j/1/.
In JavaScript, strings can be compared based on their “value”, “characters case”, “length”, or “alphabetically” order: To compare strings based on their values and characters case, use the “Strict Equality Operator (===)”.
Yes, === can compare two strings, giving true or false as a result.
Comparing strings with special characters one needs to use Function ExactMatch to compare each character exactly. In the above code snippet, VarInputString is a Variable which is compared with another string " " (Space). Function ExactMatch returns True if the value of the Variable VarInputString equals to " ".
Firstly, you are safe to compare strings that contain characters from Basic Multilangual Plane (including the ASCII characters) using regular comparison operators === , == or utility function Object.is() . Both str1 and str2 contain ASCII characters, so you can safely compare them using comparison operators.
They compare the characters of a string in alphanumeric order one by one and consider the length of the strings in the very end. consts1='javascript'; consts2='node.js'; console.log(s1>s2); // false In JS, every string has the lengthproperty. By comparing the value of this property in different strings, we’ll get to know which of them is longer.
What could cause JavaScript string compare == to return false when the strings are identical? The JavaScript equality operator == is not buggy, it does not fail. It will return true if the string to the left and right have the same content.
Double equals is the appropriate way to compare strings in Javascript, it is returning false then there may be whitespace to the left and or right of one string. Put a.trim()on the end of the strings and my comparison should started working: var panel = response.substr(0, response.indexOf("<")).trim();
If you want to do case insensitive comparison of the strings in JavaScript, you can turn both strings to lowercase and compare them using strict equality operator afterwards. consts1='javascript'; consts2='Javascript'; console.log(s1.toLowerCase() ===s2.toLowerCase()); // true Comparing the length of JavaScript strings
Unlike what some other people here have said, this has nothing to do with encodings. Rather, your two strings use different code points to render the same visual characters.
To solve this correctly, you need to perform Unicode normalization on the two strings before comparing them. Unforunately, JavaScript doesn't have this functionality built in. Here is a JavaScript library that can perform the normalization for you: https://github.com/walling/unorm
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With