Javascript String Compare == sometimes fails

后端 未结 9 2018
你的背包
你的背包 2020-12-05 17:25

How could the following code sometimes evaluate to false?

(transport.responseText == \'1\' || 
 transport.responseText == \'CARD_VALID\')

M

相关标签:
9条回答
  • 2020-12-05 18:17

    I had the same problem and i noticed that i was comparing two objects

    to solve this issue i had to use

    JSON.stringify(user._id) === JSON.stringify(userId) // true 
    
    0 讨论(0)
  • 2020-12-05 18:19

    If you want something a little less complicated and you are dealing with NUMERIC VALUES, use

    parseFloat()
    

    works like a charm

    0 讨论(0)
  • 2020-12-05 18:22

    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
    }
    
    0 讨论(0)
提交回复
热议问题