Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysterious javascript behaviour: unequal equals

Tags:

javascript

I really don't know how is it possible, and I couldn't reproduce the error in a simplified environment, say JSFiddle. But here is how it looks in my application.

I'm trying to convert value that can be 'True', 'False' or 'something else' string into boolean if it is either True or False. In most cases it works just fine (although they may be a slicker way of doing the conversion) as it is shown on the picture below

enter image description here

however on occasion it fails and the value of the watch shows as follow:

enter image description here

So suddenly string variable with a value of "True" is not equal to a "True" string literal. I'm lost. How is it possible? What am I missing here?

Thanks in advance

like image 655
michal Avatar asked Oct 09 '22 16:10

michal


1 Answers

Maybe your strings contain characters that your debugging tools don't show. Here's an example in Google Chrome's Console:

> var a = "foo";
> var b = "foo\r";
> a
"foo"
> b
"foo"
> a === b
false

It is really difficult to say what's going on in your case but you could work with the .length and .charAt(i) properties of the 'mysterious' strings to find out what's going on.

like image 94
Josef Pfleger Avatar answered Oct 12 '22 12:10

Josef Pfleger