Why is my variable true and not true at the same time?

Just take a look at this javascript:

localStorage.myAwesomeItem = true;
var item = localStorage.myAwesomeItem;
alert(item);
if(item==true)
    {alert("really true");}
else
    {alert("lies; not true");}

jsfiddle

I installed myAwesomeItemlocal storage on true. Good. Then I save this element in a variable called item. And an alert to check its meaning. As you can see, this true.

Then I check the condition if my element is really right. But this is not so. He is going for else.

Can someone explain me this behavior?

+4
source share
2 answers

Local storage converts everything that is stored into a string. Therefore, you can make it work by doing this:

if(item=="true")...
+6
source

Localstorage . , "true", false - "false".

javascript true.

+6

All Articles