var test = null;
if(test !== null){
console.log('should not be logged in the console');//it worked
}
localStorage.setItem('foo',null);
console.log(localStorage.getItem('foo'));//logs null
if(localStorage.getItem('foo') !== null){
console.log('should not be logged');//din't work, it's getting logged in the console
}
It seems the localStorage is storing the value null as string 'null'. So, the following code worked fine for me.
if(localStorage.getItem('foo') !== 'null'){
I have also ensured the code worked for me with setting the localStorage value something other than null.
This is actually not an answer. Because we may set localStorage value as string 'null' too. Not?
I know I can check like if(!variable){
but this will check for empty strings (""), null, undefined, false and the numbers 0 and NaN.
And there's a way to check for null only using like this:
if(variable === null && typeof variable === "object")
This might be a bug to Storage system? Is there any solution for checking actually null instead of 'null'?