Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to clear session storage data with specified session storage item ?

  • Last Updated : 22 Jan, 2021

In this article, we are going to learn how we can clear the session storage of a browser using JavaScript by getting the specified session storage item.

We can achieve this by using Window sessionStorage( ) property. The Window sessionStorage() property is used for saving 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.



We can get specified session storage using the getItem() method.


We can clear the session storage by using the clear() method.




<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
        body {
            text-align: center;
        h1 {
            color: green;
<body style="text-align: center;">
    <h1 style="color: green;">
        How to clear session storage data 
        with getting the specified session 
        storage item?
    <input type="text" id="text">
    <button onclick="display()">
        Display my item
    <p id="display"></p>
    <button onclick="isEmpty()">
        Checking if Empty
    <p id="isEmpty"></p>
        // 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
                    .innerText = 'Key does not exist';
            } else {
                // If it exists
                    .innerText = displayItem;
                // Clearing the session storage
        // Checking if session storage is empty
        function isEmpty() {
            // If session storage is empty
            if (sessionStorage.length == 0)
                    .innerText = 'It is empty';
                    .innerText = 'It is not empty';


Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!