Skip to Content
👆 We offer 1-on-1 classes as well check now

Local Storage

Local Storage allows websites to store data directly in a user’s web browser. This data persists even after the browser is closed and reopened, making it a powerful tool for remembering user preferences and other information.

What is Local Storage?

Local Storage is a web storage object provided by web browsers. It’s like a small, key-value database that’s specific to the domain of the website. Think of it as a place to save small pieces of information on your computer, accessible only by the website that saved it. It’s different from cookies in that it offers more storage space and doesn’t automatically send data to the server with every request.

Basic Example

Let’s see how to store and retrieve a simple value using JavaScript. We’ll use the localStorage object, which is available globally in the browser.

// Storing data (saving a key-value pair) localStorage.setItem("username", "JohnDoe"); // Retrieving data (getting the value associated with a key) let storedUsername = localStorage.getItem("username"); console.log(storedUsername); // Output: JohnDoe // Removing data (deleting a key-value pair) localStorage.removeItem("username"); // Clearing all stored data for this domain // localStorage.clear();

In this example:

  • localStorage.setItem("username", "JohnDoe"): This saves the value “JohnDoe” associated with the key “username”.
  • localStorage.getItem("username"): This retrieves the value associated with the key “username”.
  • localStorage.removeItem("username"): This removes the key-value pair with the key “username”.
  • localStorage.clear(): This removes all key-value pairs stored by the website. Be cautious when using this!

Practical Usage

A common use case is remembering a user’s theme preference (light or dark mode). Here’s how you could implement that:

<!DOCTYPE html> <html> <head> <title>Theme Example</title> <style> body { transition: background-color 0.5s ease; /* Smooth transition */ } .dark-mode { background-color: #333; color: #fff; } </style> </head> <body> <button id="themeToggle">Toggle Theme</button> <h1>Hello!</h1> <script> const themeToggle = document.getElementById('themeToggle'); const body = document.body; function applyTheme(theme) { body.className = theme; // Apply dark-mode class } // Check for saved theme on page load const savedTheme = localStorage.getItem('theme'); if (savedTheme) { applyTheme(savedTheme); } themeToggle.addEventListener('click', () => { if (body.classList.contains('dark-mode')) { applyTheme(''); // Remove dark-mode class localStorage.setItem('theme', ''); } else { applyTheme('dark-mode'); localStorage.setItem('theme', 'dark-mode'); } }); </script> </body> </html>

This code:

  1. Checks for a previously saved theme.
  2. Applies the saved theme when the page loads.
  3. Toggles the theme and saves the new choice in Local Storage when the button is clicked.

Key Takeaways

  • Local Storage allows you to store data directly in the user’s browser.
  • Data persists even after the browser is closed.
  • Use localStorage.setItem() to store data, localStorage.getItem() to retrieve it, and localStorage.removeItem() to delete it.
  • Common uses include saving user preferences like theme settings or language choices.
  • Data is stored as strings, so you may need to use JSON.stringify() and JSON.parse() to store and retrieve complex data structures (objects, arrays).
Last updated on