Carl
Posts: 0
Joined: Mon Nov 17, 2014 12:08 am

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

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 ?

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

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

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.

Carl
Posts: 0
Joined: Mon Nov 17, 2014 12:08 am

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

Of course! Thank you very much for your support.

Carl
Posts: 0
Joined: Mon Nov 17, 2014 12:08 am

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

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?

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

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

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.

Return to “Issues”