0

im very new to reactNative development. and trying to compare two strings.

ex: response["Result"] equalsTo "True"

i tried using == , toEqual but didnt work.

Updated

im getting JSON Response like this

{ Result: true,
  Message: 'Login Success.',
  Code: 'Login Code' }

when i console.log(response["Result"]) it prints true.

but when i compare. it always go to the else part.

if(response["Result"] == "true"){
    console.log("SUCCESS");
}else{
    console.log("ERROR");
}
6
  • 1
    react native is actually js,` ==` should be fine. Post more details how "didn't work" Commented Apr 12, 2017 at 20:38
  • What happened when you tried to use ==? Commented Apr 12, 2017 at 20:39
  • i have updated the question @PedroCastilho Commented Apr 12, 2017 at 20:44
  • @aahung i have updated the question. im working with JSON Response Commented Apr 12, 2017 at 20:47
  • 1
    The problem here is that true is not the same as "true". Commented Apr 12, 2017 at 20:47

1 Answer 1

3

Your problem is that the value of Response["Result"] is true, not "true". true is a boolean constant, "true" is a string.

Change your code to:

if(response["Result"]){
    console.log("SUCCESS");
}else{
    console.log("ERROR");
}

and it should work.

Sign up to request clarification or add additional context in comments.

1 Comment

got it. it worked. when im using native programming java or Objective C im treating it like string and then convert to boolean. but here its taking directly as boolean. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.