How to get data from localstorage in javascript


How to get data from localstorage in javascript

Hi friends, In this tutorial, you will learn how to get data from localstorage in javascript. This is a very important feature and is required in web development. It stores the data in a storage variable with the help of javascript and that data will remain for the future session. Data stored in local storage can not be deleted even after closing the browser. It will be shown each time you open the browser if you set the data in a local storage variable.

Also read, Allow only numbers in textbox javascript

Below is an example to get data from localstorage in javascript

<html>
<head>
<title>Get data from session storage in javascript</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">

</head>
<body>
  <h3>Get Data From Local Storage Using Javascript</h3>
  <p id="demo" style="font-weight:bold;"></p>
  <script type="text/javascript">
    localStorage.setItem("username",'My name is XYZ');
    
    document.getElementById("demo").innerHTML = localStorage.getItem("username");
  </script>
</body>
</html>

Explanation:-

  • localStorage.setItem() is used to set the value in a local storage variable.
  • localStorage.getItem() is used to get the value of the variable stored on the local storage.

If you notice the above example then you know that I have set the value “My name is XYZ” in the first parameter of the setItem() method. On the other hand, I have used this parameter in the getItem() method to get the value of that variable.

Output:-

How to get data from localstorage in javascript

Conclusion:- I hope this tutorial will help you to understand the overview. If there is any doubt then please leave a comment below.


Leave a Comment