Vote Up 0 Vote Down

javascript string comparison not working

I am comparing two strings in javascript and I know that both are same.

   str1 = "hello";
//str2 is from an ajax response
if(str1 == str2){
alert("Same");
}
else{
alert("Not Same");
}


I am getting str2 value from an ajax operation. Both are same. But it is not working.
flag

Answers


Vote Up 0 Vote Down
Probably the string from the ajax response may have white spaces. You need to trim the string and compare. Use the trim function in jquery to easily trim the string variable.

$.trim(str2);


This will remove extra whitespaces from the string. It is always safety to validate and trim the incoming responses.
flag | link |

Your Answer

Login before answering

Login with facebook