Local storage - boolean


In many browsers local storage can only store string. So when we store the boolean true or false, it actually stores the strings "true" or "false". In order to get back the real boolean values, we can use the JSON.parse() method.

examples/js/local_storage_boolean.html
Local storage - store boolean

<script>
console.log('Start');

var cond = localStorage.getItem('cond');
if (cond === null) {
    console.log('was null setting to false');
    cond = false;
} else {
    cond = JSON.parse(cond)
}

console.log(cond);
cond = ! cond;
console.log(cond);
localStorage.setItem("cond", cond);

console.log('End');
</script>