Retrieve Item from localStorage

Retrieves a value from localStorage by its key and parses it as JSON.

Contributed by @ditinagrawal

javascript
function getFromLocalStorage(key) {
  const item = localStorage.getItem(key);
  return item ? JSON.parse(item) : null;
}
javascript
getFromLocalStorage("user"); // { name: "John", age: 30 }
GitHubEdit on GitHub