Harshit
Harshit

Reputation: 397

How to compare string variable using JavaScript

I am trying to compare the variable using javascipt: response value: ""[email protected]""

response value i am getting it from server.

  var str1="[email protected]"
    var str2 =response;

if(str1===str2)
{
//
}

However not getting the proper result. any idea on how to compare them ?

Upvotes: 1

Views: 89

Answers (2)

Vamsi Krishna K
Vamsi Krishna K

Reputation: 11

You are trying to compare '""[email protected]""' with '[email protected]'. They would never be equal.
Actually ""[email protected]"" is not a valid string. It might have been represented as '""[email protected]""' and "[email protected]" is a valid string (Same as '[email protected]').

Upvotes: 0

smaili
smaili

Reputation: 1235

There are a few ways to achieve your goal:

1) You can remove all " from the response when doing your equality check:

if(str1===str2.replace(/['"]+/g, ''))
{
//
}

2) Change your server code to not include ". Doing so, would mean that your Javascript will not need to change.

3) Last option, add " to your str1:

var str1='"[email protected]"'
var str2 =response;

if(str1===str2)
{
//
}

Obviously I don't know enough about your requirements to tell you which one you should do, but my suggestion would be choice #2 because I think it's strange to return an email address wrapped in quotes, otherwise I would recommend #1.

Upvotes: 2

Related Questions