I just want something that would evaluate to false
You could use an empty string instead to mark the unset value:
sessionStorage.setItem('key','');
This way you can re-use your current check:
if (!sessionStorage.getItem('key')) {
// do something
}
That said, you mentioned you had a drop down to select values, and that's why you need such a value, but I think a solution involving deleting the item on the null value would be better. Here's a working demo, and the code used to make it:
var dd = document.getElementById('dropdown');
dd.addEventListener('change', function(){
var value = this.value;
if (value === 'null')
sessionStorage.removeItem('key');
else sessionStorage.setItem('key', value);
});
var check = document.getElementById('check');
check.addEventListener('click', function(){
this.innerHTML = 'Value: '+sessionStorage.getItem('key')+
', check: '+(!sessionStorage.getItem('key'));
});
<select id="dropdown">
<option value="null">none</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
<button id="check">Check value</button>
sessionStorage.removeItem('key')
instead ofsessionStorage.setItem('key','null')
? – Cutcherrykey
. it's binded to an event listerner, so it would be odd in one case to do a delete and in all other case to set the value no? – Paymentnull
,not set
,0
or whatever you choose, or just remove the item and check that it's not set. – Outdoor