0

I am testing to see if a localstorage key exists or not.

So first I've done a console.log to see what's inside the value...

console.log(localStorage.getItem('mykey'));

This returns null

So I've done this for a condition:

if (localStorage.getItem('mykey') === null) {
    console.log('It is null');
}

But although I know it's null it's not showing:

console.log('It is null');

What could the problem be?

5 Answers5

1

Updated

The original posted localStorage.getItem('mykey') === null actually returns true on Chrome, so it might be a browser related issue.

So by using the first suggested solution with double equal ==, one might get a better crossbrowser support, though the 2nd/3rd options might be even better.

If the key doesn't exist, the returned value is an object being null, which is not the exact same type (===) as null

Either drop one = (and it will do an auto-cast) and it will work

if (localStorage.getItem('mykey') == null) {
  console.log('It is null');
}

or simply do something like e.g.

if (!localStorage.getItem('mykey')) {
  console.log('It is null');
}

or (as shown here) which might be even better (and could be a dupe actually)

if (!localStorage.hasOwnProperty('mykey')) {
  console.log('It is null');
}
Asons
  • 84,923
  • 12
  • 110
  • 165
  • I don't think the first suggestion is correct. `localStorage.getItem('randomkey') === null` is true (tested on Chromium 66) – Aurélien Bottazini Jun 20 '18 at 09:46
  • @AurélienBottazini With Version 67.0.3396.87 (Official Build) (64-bit) it also returns true ... hmmm, will update my answer, as it appears this might be a browser issue then. Thanks for the comment – Asons Jun 20 '18 at 09:51
  • @AurélienBottazini Updated my answer with something that might be more appropiate. – Asons Jun 20 '18 at 09:56
0

If you set localStorage.setItem('someKey', null) and then if you will try get and check someKey value like this localStorage.getItem('someKey') === null comparison result will false you need use JSON.parse() method before comparison with null like this JSON.parse(localStorage.getItem('someKey')) === null

0

Check if localStorage is null.If localStorage is null the control won't enter the if block.

0

Hypothesis on what is happening:

localStorage.setItem('mykey', null)
console.log(localStorage.getItem('mykey')) // returns "null"

So explicitly setting mykey to null actually sets it to the "null" string.

For that comparaison to work you could do

if (localStorage.getItem('mykey') === 'null') {
  console.log('It is null');
}
Aurélien Bottazini
  • 3,249
  • 17
  • 26
0

The identity (===) operator checks whether both the operands are equal or identical in both value and datatype. That is,

var a = 2;
if (a === '2') 

- will return false, since a is integer and '2' is a string. Where as,

if (a == 2)

- will return true, since '==' operator doen't enforce datatype eqality.

So in order to handle all situations like, the key is missing from localStorage or it is explicitly set to null, you should use '==' operator.