Javascript String Compare == sometimes fails
A1 = "speed"
A2 = "speed"
if(A1 == A2) => Error !!!
USE THIS TEST IN CONSOLE:
escape("speed")
result: "speed"
escape(A1)
result: "speed%0D" => This is the problem %0D !!!
escape(A2)
result: "speed" => OK !!!
Use correct code:
if(A1.slice(0, -1) == A2) This is OK!
I had a similar problem where two obviously identical strings would not be equal, and I was pulling my hair out trying to solve it, so I did this:
for (var c=0; c<string_1.length; c++) {
if (string_1.charCodeAt(c) != string_2.charCodeAt(c)) {
alert('c:'+c+' '+string_1.charCodeAt(c)+'!='+string_2.charCodeAt(c));
valid = false;
}
}
And I found that the last character on one string was 10, and the last character on the other was 13, I thought both strings were null terminated, but they were not.
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(panel == "combo"){
//do something
}