BluGeni
BluGeni

Reputation: 3454

parse json and then compare with if statement not working

I am not sure what I am doing wrong here.

the json (response):

Response: {"success":true,"message":"Registration Success, check your email to validate your account so you can login"} 

My code:

        console.log("Response: "+response);
        obj = JSON.parse(response);
alert(obj.success);
        if (obj.success == 'true')
{
        console.log("Response:success detected ");
alert('hi');

}

I never get inside the if statement even though alert(obj.success) gives me an alert with 'true'. what is making my if statement not function correctly?

Upvotes: 1

Views: 1134

Answers (1)

Ryan Bigg
Ryan Bigg

Reputation: 107728

It's not 'true', it's true. Remove the quotes.

In a JavaScript console:

'true' == true
=> false
true == true
=> true
'true' == 'true'
=> true

You may have thought this would be the case because in languages like PHP, true does indeed == 'true'.

Upvotes: 6

Related Questions