English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

How to retrieve elements from local storage in JavaScript?

To from local storageRetrieve element, we must check if the browser recognizes local storage. Later, we must set an element in local storage. Currently, we must be careful because once the browser is closed, no data will be stored in local storage. Later, we need to retrieve the data stored in local storage. Let's discuss it briefly.

To set data in local storage, the following syntax is used

localStorage.setItem();

Example

In the following example, first, use the if condition to check the storedCompatibility . Later, uselocalStorage.setItem()Place the item in local storage. Once the item is in local storage, you can use the propertylocalStorage.getItem()Retrieve data.

<html>
<body>
<p id="storage"></p>
<script>
   if (typeof(Storage) !== "undefined") {
      localStorage.setItem("product", "Tutorix");
      document.getElementById("storage").innerHTML = localStorage.getItem("product");
   }
   else {
      document.getElementById("storage").innerHTML = "Sorry, no Web Storage compatibility...";
   }
</script>
</body>
</html>

Output Result

Tutorix