i am trying to invert a boolean value (toggle);
var current_state=$.cookie('state');
if(current_state==null) {
current_state=false;
}
console.log(current_state);
current_state=(!current_state);
console.log(current_state);
$.cookie('state', current_state, { expires: 7, path: '/' });
In my opinion the second console.log should display the opposite of the first one, but both calls report false. Whats up here?
Your
$.cookie('state')seems to be a string (“false“).As a proof, see the following code (also in this jsfiddle):
It outputs
falsetwice.Why? Because you check if it is null and do not support other cases.
You can do something like this:
and your code will output boolean
trueorfalsefirst and then the opposite boolean value.