Skip to content

Instantly share code, notes, and snippets.

@Overv
Created October 15, 2013 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Overv/6992680 to your computer and use it in GitHub Desktop.
Save Overv/6992680 to your computer and use it in GitHub Desktop.
Yet another JavaScript WTF

The local storage functionality in browsers is a bit limited and this can lead to some rather surprising behaviour.

localStorage[0] = false;

if (localStorage[0]) {
    console.log('wtf'); // runs?!
}

When checking the value stored in localStorage, it appears that the boolean was silently converted to the string "false", which is truthy.

Turns out that this is one of those cases where it pays off to carefully read the specification, which states that local storage only accepts string values!

If you want to store an object or other type of value, you can serialize the data with JSON.stringify and load it again with JSON.parse.

@Overv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment