DOM Storage.getItem() Method
The getItem() method reads the value stored under a key in localStorage or sessionStorage, returning null if there is none.
Description
The getItem() method returns the string stored under a key, or null if there's nothing there. Use JSON.parse() for values that were stored as JSON, and ?? to provide a default.
Because missing keys return null rather than throwing, passing the result straight to JSON.parse() gives null for missing data, which is often what you want. Wrap the parse in try...catch in case the stored text has been corrupted or edited by hand.
Storage access can throw, for example in some private browsing modes or in sandboxed iframes, so wrap calls in try...catch where it matters. Both localStorage and sessionStorage support the method, and changes made in one tab fire a storage event in other tabs on the same origin.
Syntax
storage.getItem(keyName)Parameters
- keyNameThe key to read.
Return Value
The stored string, or null if the key doesn't exist.
Example
Browser Support
The following information will show you the current browser support for the Storage.getItem() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





