Skip to content
Related Articles

Related Articles

HTML | DOM Storage setItem() Method

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 26 Jul, 2019

The setItem() method is used to set the storage object item which is specified by the user. This storage object can be a localStorage object or sessionStorage object.

For local storage:

localStorage.setItem(keyname, value)

For session storage:

sessionStorage.setItem(keyname, value)

Parameters: Two parameters are required :-

  • Keyname: It specifies the name of the key used for getting the value.
  • value: It specifies the value which replaces the old value.

Return Value: A String, representing the inserted value.


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




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

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!