HTML5 LocalStorage: Checking if a key exists

The Solution to HTML5 LocalStorage: Checking if a key exists is


Quoting from the specification:

The getItem(key) method must return the current value associated with the given key. If the given key does not exist in the list associated with the object then this method must return null.

You should actually check against null.

if (localStorage.getItem("username") === null) {
  //...
}

~ Answered on 2013-04-15 08:35:27


Most Viewed Questions: