I check the response from the server, where the string surname comes from in the variable data . As a result, I have a very strange picture:

 console.log(typeof(data)) // string console.log(data) // surname var f = (data === "surname"); console.log(f) // false; 

Save How to find a bug?

  • As an option to get your result, declare var data = "surnаme"; - here the letter "a" is Cyrillic. And in all your checks in Latin. - Sergiks
  • 2
    Space in the string that comes from the server, no? - Sapphiron
  • This option has already been checked and still so - Ordman
  • @Sapphiron, thank you so much)) - Ordman
  • Look character by character what the string consists of: console.log( data.split("").reduce(function(p,c){p.push( c.charCodeAt(0).toString(16)); return p;}, []).join(':')); - will output the hexadecimal codes of each of the characters of the line through a colon: 73:75:72:6e:61:6d:65 - Sergiks

3 answers 3

Possible space in the line. If it is not always known how exactly the data can come (for example, they are programmed from somewhere), you can trim the spaces with str.trim() .

    It is strange that the localeCompare method is not mentioned (not used) (for more details, see here ) or the universal match ()

    • 2
      How will these methods help with the problem in question? - Grundy

    You have a strict comparison here ( === ):

     data === surname 

    And you need a loose ( == ):

     data == surname 
    • one
      Better not to use a loose comparison at all - Alexey Kapustsin