How to Clear Session Storage Data with Specified Session Storage Item? Last Updated : 25 Jun, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report JavaScript sessionStorage is a web storage technique that allows you to store data in a web browser. You can manually set and retrieve the values from it using some methods. The sessionStorage stores the data for a short amount of time until the browser tab or window gets closed. We will learn how to clear the session storage of a browser using JavaScript by getting the specified session storage item. Using Window sessionStorage( ) propertyWe can achieve this by using the Window sessionStorage( ) property. The Window sessionStorage() property saves key/value pairs in a web browser. It stores the key/value pairs in a browser for only one session and the data expires as soon as a new session is loaded. Syntaxwindow.sessionStorage We can clear the session storage by using the clear() method. sessionStorage.clear()Example: This example we demonstrates how to manage session storage. It allows users to display, check if the session storage is empty, and clear items. Initial items are set in session storage for demonstration. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> body { text-align: center; } h1 { color: green; } </style> </head> <body style="text-align: center;"> <h1 style="color: green;"> GeeksforGeeks </h1> <h4> How to clear session storage data with getting the specified session storage item? </h4> <input type="text" id="text"> <button onclick="display()"> Display my item </button> <p id="display"></p> <button onclick="isEmpty()"> Checking if Empty </button> <button onclick="clear()"> clear my item </button> <p id="isEmpty"></p> <script> // Setting items in the local storage sessionStorage.setItem('item1', 'Kotlin'); sessionStorage.setItem('item2', 'Flutter'); sessionStorage.setItem('item3', 'React'); function display() { // Getting the text value of input field let item = document.getElementById('text').value; // Getting particular item from the // session storage let displayItem = sessionStorage.getItem(item); // Checking if key exists or not in // the session storage if (displayItem == null) // If key doesn't exist { document.getElementById('display') .innerText = 'Key does not exist'; } else { // If it exists document.getElementById('display') .innerText = displayItem; // Clearing the session storage sessionStorage.clear(); } } function clear() { sessionStorage.clear(); } // Checking if session storage is empty function isEmpty() { // If session storage is empty if (sessionStorage.length == 0) document.getElementById('isEmpty') .innerText = 'It is empty'; else document.getElementById('isEmpty') .innerText = 'It is not empty'; } </script> </body> </html> Output: Comment More infoAdvertise with us Next Article Managing Local Storage & Session Storage using React Hooks C chetankhanna767 Follow Improve Article Tags : JavaScript CSS-Misc HTML-Misc JavaScript-Misc JavaScript-Questions +1 More Similar Reads How to Save Data in Session and Local Storage [Full Guide] When working with web applications, session storage and local storage are essential tools for storing data on the client side. These storage mechanisms allow you to persist user data between page reloads or sessions, helping improve user experience and performance. Session storage is useful for temp 9 min read Managing Local Storage & Session Storage using React Hooks To manage Loacal Storage and Session Storage, we can use hooks like useEffect and useState provided by React. Managing local or session storage is a repetitive task, so it is a good practice to create a custom hook that manages the storage properly. In this article, I'll cover the whole process of m 3 min read Difference Between Local Storage, Session Storage And Cookies The HTTP protocol is one of the most important protocols for smooth communication between the server and the client. The main disadvantage of the HTTP protocol is that it is a stateless protocol, which means it does not track any kind of response or request by the server or the client. So, to resolv 6 min read How to set & unset session variable in codeigniter ? The session class in CodeIgniter allows the user to maintain a userâs âstateâ and track their activity while browsing the website. The session can be initialized using the library and auto-loaded in the environment using the following command. $this->load->library('session'); Set the session v 2 min read How to delete specific cache data in ReactJS ? In React, we can access all cache data from the browser and delete specific cache data from the browser as per the user's requirement whenever needed using the Cache Storage of the browser. Caching is a technique that helps us to store a copy of a given resource in our browser and serve it back when 2 min read How to Set Cookies Session per Visitor in JavaScript? Managing session cookies in JavaScript is essential for web developers to maintain user state and preferences across multiple sessions. The document. cookie property provides a basic mechanism for cookie management, utilizing JavaScript libraries or frameworks can offer enhanced security and flexibi 4 min read Like