JS Web Storage API: Your Guide to Storing Data in the Browser 🎯

beginner
15 min

JS Web Storage API: Your Guide to Storing Data in the Browser 🎯

Welcome to our comprehensive guide on the JavaScript Web Storage API! This tutorial is designed to help both beginners and intermediates understand how to store data in a browser using this powerful tool. 📝

What is the Web Storage API?

The Web Storage API allows web applications to store data in the user's browser. Unlike cookies, which have a limited size and can only store simple data, the Web Storage API offers larger storage capacity and the ability to store complex data types. 💡

Key Terms 📝

  • Session Storage: Data stored in session storage is tied to the current browser session and is lost when the page is closed or the user navigates away.
  • Local Storage: Data stored in local storage persists even after the browser is closed and reopened, or the user navigates away from the site.

Setting Up the Storage 🎯

Accessing the Storage

To access the storage, we use the window.localStorage or window.sessionStorage properties.

javascript
// Accessing local storage let storage = window.localStorage; // Accessing session storage let session = window.sessionStorage;

Storing Data

To store data, we use the setItem() method.

javascript
// Storing data in local storage storage.setItem('name', 'John Doe'); // Storing data in session storage session.setItem('score', 85);

Retrieving Data

To retrieve data, we use the getItem() method.

javascript
// Retrieving data from local storage let name = storage.getItem('name'); console.log(name); // John Doe // Retrieving data from session storage let score = session.getItem('score'); console.log(score); // 85

Updating Data

To update data, we can retrieve the existing data, modify it, and then store it back using setItem().

javascript
// Retrieve data and update it let currentScore = session.getItem('score'); currentScore++; session.setItem('score', currentScore);

Removing Data

To remove data, we use the removeItem() method.

javascript
// Removing data from local storage storage.removeItem('name'); // Removing data from session storage session.removeItem('score');

Checking if Data Exists

To check if data exists, we can use the length property.

javascript
// Check if data exists in local storage if (storage.length > 0) { console.log('Data exists'); } else { console.log('No data'); }
Quick Quiz
Question 1 of 1

What is the difference between local storage and session storage?

Real-world Application 🎯

Let's create a simple to-do list application using local storage.

javascript
// Initialize an array to store tasks let tasks = []; // Function to add tasks function addTask(task) { tasks.push(task); saveTasks(); } // Function to save tasks in local storage function saveTasks() { localStorage.setItem('tasks', JSON.stringify(tasks)); } // Function to retrieve tasks from local storage function loadTasks() { tasks = JSON.parse(localStorage.getItem('tasks')); } // Load tasks on page load loadTasks();

Now, whenever a user adds a task, we save it to local storage using the saveTasks() function. When the page loads, we load the tasks from local storage using the loadTasks() function.

This way, the user's tasks are saved and loaded even if they close the browser and reopen it later. 🚀