LocalStorage & SessionStorage


Web Storage offers powerful client-side data storage capabilities directly within a user's web browser, enhancing web applications by allowing them to store data persistently or for the duration of a session. This is a significant improvement over cookies for larger data sets.

localStorage and sessionStorage

These are two primary types of web storage. Both store data as key-value pairs, but they differ in their persistence. localStorage retains data even after the browser is closed, making it ideal for long-term user preferences or offline data. sessionStorage, on the other hand, stores data only for the duration of the browser session and is cleared when the tab or browser is closed.


Example 1: Setting a localStorage item

<!DOCTYPE html>
<html>
<head>
    <title>localStorage Example</title>
</head>
<body>
    <script>
        // Set a key-value pair in localStorage
        localStorage.setItem('username', 'webDevUser'); 
        console.log('Username set in localStorage.');
    </script>
</body>
</html>

Explanation This HTML code demonstrates how to use localStorage.setItem() to store a string value associated with a key. This data will persist across browser sessions.


Example 2: Retrieving a localStorage item

<!DOCTYPE html>
<html>
<head>
    <title>localStorage Retrieval</title>
</head>
<body>
    <script>
        // Retrieve the 'username' from localStorage
        const user = localStorage.getItem('username'); 
        if (user) {
            console.log('Retrieved username from localStorage: ' + user);
        } else {
            console.log('Username not found in localStorage.');
        }
    </script>
</body>
</html>

Explanation This example shows localStorage.getItem() in action, retrieving the previously stored 'username'. It's crucial for accessing persistent user data.


Example 3: Removing a localStorage item

<!DOCTYPE html>
<html>
<head>
    <title>localStorage Removal</title>
</head>
<body>
    <script>
        // Remove the 'username' item from localStorage
        localStorage.removeItem('username'); 
        console.log('Username removed from localStorage.');
    </script>
</body>
</html>

Explanation localStorage.removeItem() is used here to delete a specific key-value pair from localStorage. This is useful for clearing outdated or sensitive user data.


Example 4: Setting a sessionStorage item

<!DOCTYPE html>
<html>
<head>
    <title>sessionStorage Example</title>
</head>
<body>
    <script>
        // Store a session-specific preference
        sessionStorage.setItem('theme', 'dark'); 
        console.log('Theme set in sessionStorage for this session.');
    </script>
</body>
</html>

Explanation This code snippet illustrates sessionStorage.setItem(), storing data that will only last until the user closes the browser tab or window, ideal for temporary session states.


Example 5: Retrieving a sessionStorage item

<!DOCTYPE html>
<html>
<head>
    <title>sessionStorage Retrieval</title>
</head>
<body>
    <script>
        // Retrieve the 'theme' from sessionStorage
        const currentTheme = sessionStorage.getItem('theme'); 
        if (currentTheme) {
            console.log('Current theme from sessionStorage: ' + currentTheme);
        } else {
            console.log('Theme not found in sessionStorage.');
        }
    </script>
</body>
</html>

Explanation Here, sessionStorage.getItem() is used to retrieve data that is valid only for the current Browse session. This is excellent for managing temporary user interactions.