HTML | DOM Storage getItem() Method

The getItem() method is used to retrieve the storage object which is specified by the user. This storage object can be localStorage object or sessionStorage object.

Syntax:

    For local storage:

    localStorage.getItem(keyname)

    For session storage:

    sessionStorage.getItem(keyname)

Parameters: It requires Keyname which specifies name of the key used for getting the value.

Return Value: A String, representing the value of the specified key.

Example: Get item from local storage.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <!--script for creating new local 
      storage item and retrieve it -->
    <script>
        function createItem() {
            localStorage.name = "GeeksforGeeks";
        }
  
        function myFunction() {
  
            // get name of item from local storage.
            var x = localStorage.getItem("name");
            document.getElementById("demo").innerHTML =
                x;
        }
    </script>
</head>
  
<body>
    <h1>Welcome to GeeksforGeeks</h1>
    <h3>The Storage getItem() Method</h3>
    <p>
      Click on button to create a 
      local storage item 
    </p>
  
    <button onclick="createItem()">
      Create local storage item
    </button>
  
    <p>
      Click the button to get the item value:
    </p>
  
    <button onclick="myFunction()">
      Get the item value
    </button>
  
    <p id="demo"></p>
  
</body>
  
</html>

chevron_right


Output:
Before:

After:

Supported Browsers: The browser supported by DOM getItem() Method are listed below:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.