Both direct access to the property ( localStorage.item or localStorage['item'] ) and the use of the functional interface ( getItem('item') ) work fine. Both standards and cross browser are compatible. * According to specification :
The names of the supported properties of the Storage object are the keys of each key / value pair currently present in the list associated with the object in the order in which the keys were last added to the storage area.
They simply behave differently when no key / value pairs with the requested name are found. For example, if the key 'item' does not exist, var a = localStorage.item; will result in a undefined , whereas var a = localStorage.getItem('item'); will result in a null . As you discovered, undefined and null are not interchangeable in JavaScript / EcmaScript. :)
EDIT: As Christoph points out in his answer , a functional interface is the only way to securely store and retrieve values ββunder keys equal to the predefined localStorage properties. (There are six of them: length , key , setItem , getItem , removeItem and clear .) So, for example, the following will always work:
localStorage.setItem('length', 2); console.log(localStorage.getItem('length'));
In particular, note that the first statement will not affect the localStorage.length property (with the possible exception of increasing it if localStorage did not already have the 'length' key). In this regard, the specification seems internally contradictory.
However, the following probably will not do what you want:
localStorage.length = 2; console.log(localStorage.length);
Interestingly, the first is non-use in Chrome, but it is synonymous with a functional call in Firefox. The second will always register the number of keys present in localStorage .
* This is true for browsers that primarily support web storage. (This applies to almost all modern desktop and mobile browsers.) For environments that simulate local storage using cookies or other methods, the behavior depends on the pad used. Several polyfills for localStorage can be found here .