DEV Community

Cover image for Web Storage Api
Amrasakpare Lawrence
Amrasakpare Lawrence

Posted on

Web Storage Api

_This is a submission for DEV Challenge v24.03.20, One Byte Explainer: Web Storage Api


The Web Storage API helps websites save stuff in your browser, kind of like big, fancy cookies. LocalStorage keeps things even after you close your browser, while SessionStorage only keeps them for your current browsing session. This helps sites remember your choices, run faster, and sometimes work without internet (but not always).

Additional Context

Here's a simple example of how you can use the Web Storage API in JavaScript 👇🏽

// Storing data using localStorage
localStorage.setItem('username', 'exampleUser');
localStorage.setItem('isLoggedIn', true);

// Retrieving data from localStorage
const username = localStorage.getItem('username');
const isLoggedIn = localStorage.getItem('isLoggedIn');

console.log(username); // Output: exampleUser
console.log(isLoggedIn); // Output: true

// Removing data from localStorage
Enter fullscreen mode Exit fullscreen mode

This code snippet demonstrates how to store, retrieve, and remove data using the localStorage object from the Web Storage API.

Top comments (0)