Page 1 of 1

Why doesnt bool = !bool work in appery.io javascript ?

Posted: Mon Nov 17, 2014 12:08 am
by Carl

Trying to perform simple task of toggling a locally stored variable and missing something. I have the following logic under a button click:

var status = localStorage.getItem('sendLocation');
console.log('retrieved status =',status);
status = !status; // this is where the trouble arises
console.log('new status=',status);
localStorage.setItem('sendLocation',status);

When I look at console I see the following after clicking a few times -
retrieved status = true
new status= false
retrieved status = false
new status= false
retrieved status = false
new status= false
... and so on.

It is as though the ! option to toggle boolean is not interpreted correctly. It works once and then no more. This is the only code using the local storage var. Any ideas ?


Why doesnt bool = !bool work in appery.io javascript ?

Posted: Mon Nov 17, 2014 5:29 am
by Yurii Orishchuk

Hi Carl,

There is a string type stored in local storage.

So you need to convert "string" - "boolean" before.

Here is a code:

pre

var status = localStorage.getItem('sendLocation');
status = status == "true";
console.log('retrieved status =',status);
status = !status; // this is where the trouble arises
console.log('new status=',status);
localStorage.setItem('sendLocation',status);

/pre

Regards.


Why doesnt bool = !bool work in appery.io javascript ?

Posted: Mon Nov 17, 2014 6:39 am
by Carl

Of course! Thank you very much for your support.


Why doesnt bool = !bool work in appery.io javascript ?

Posted: Mon Nov 17, 2014 6:57 am
by Carl

Actually - I just tried what you suggested and it didnt work either.

Im assuming there is a simple way to toggle a session variable (that I declare as boolean) via a simple button click? Any suggestions?


Why doesnt bool = !bool work in appery.io javascript ?

Posted: Mon Nov 17, 2014 10:50 pm
by Yurii Orishchuk

Hi Carl,

Yes, there was issue with variable type.

Okay here is a worked and tested code:

pre

var status = localStorage.getItem('sendLocation');
var newStatus = status == "true";
console.log('retrieved newStatus =', newStatus + ", type = " + typeof(newStatus));
newStatus = !newStatus;
console.log('new status=' + newStatus);
localStorage.setItem('sendLocation', newStatus);

/pre

Regards.