Looking at the Apple , Mozilla and Microsoft documentation, the functionality seems to be limited to handle only string key/value pairs.
A workaround can be to stringify our object before storing it, and later parse it when we retrieve it:
A little improve to Justin's variant:
Because of short-circuit evaluation , getObject() will immediately return null if key is not in Storage. It also will not throw a SyntaxError exception if value is "" (the empty string; JSON.parse() cannot handle that).
UPD. Added variable that Mark Storer mentioned in comment
We might find it useful to extend the Storage object with these handy methods:
This way we get the functionality that we really wanted even though underneath the APWE only supports strings.
Extending the Storage object is an awesome solution. For my API, WE have created a facade for localStorage and then check if it is an object or not while setting and getting.
There is a great library that wraps many solutions so it even supports older browsers called jStorage
We can set an object