HTML Window sessionStorage Property

The HTML Window sessionStorage property allow us to store key/value pairs data in a web browser only for one session that means the data is deleted when the browser tab is closed.


Following is the syntax −


Let us see an example of HTML Window sessionStorage Property −


 Live Demo

<!DOCTYPE html>
   body {
      color: #000;
      height: 100vh;
      background-color: #8BC6EC;
      background-image: linear-gradient(135deg, #8BC6EC 0%, #9599E2 100%) no-repeat;
      text-align: center;
   .btn {
      background: #db133a;
      border: none;
      height: 2rem;
      border-radius: 2px;
      width: 30%;
      display: block;
      color: #fff;
      outline: none;
      cursor: pointer;
      margin: 1rem auto;
<h1>HTML Window sessionStorage Property Demo</h1>
<button onclick="store()" class="btn">Store 'John Smith' Name to sessionStorage</button>
<button onclick="display()" class="btn">Display sessionStorage stored value</button>
<div class="show"></div>
   function store(){
      document.querySelector('.show').innerHTML='Name store successfully';
   function display(){
      var firstName=sessionStorage.getItem('firstName');
      var lastName=sessionStorage.getItem('lastName');
      document.querySelector('.show').innerHTML='Name stored in sessionStorage is: '+ firstName +' '+lastName;


Click on “Store ‘John Smith’ Name to sessionStorage” button to store name to sessionStorage:

Now click on “Display sessionStorage stored value” button to display the stored value −

Now you can also open session storage in your dev tools to understand how it works −