Horje
What is LocalStorage & SessionStorage Objects in JavaScript ?

LocalStorage and sessionStorage are Web Storage API objects in JavaScript for key-value data storage. localStorage persists data across browser sessions, while sessionStorage is limited to the current session, clearing on browser/tab closure. Both are associated with the origin and provide simple ways to store and retrieve data.

Some methods are used to deal with local and session storage:

  • setItem(): This function is used to set the field and its respective value in the browser storage.
  • getItem(): This function is used to get or fetch the value of the field that was stored initially in the browser storage.
  • removeItem(): This function is used to delete or remove the field and its respective value from the storage permanently.

Example for setting the localStorage in the browser:

// Storing data in local storage
localStorage.setItem('name', 'anyName');

// Retrieving data from local storage
const username = localStorage.getItem('name');
console.log(username); // Output: anyName

// Removing data from local storage
localStorage.removeItem('name');

Example for setting the sessionStorage in the browser:

// Storing data in session storage
sessionStorage.setItem('auth', 'true');

// Retrieving data from session storage
const theme = sessionStorage.getItem('auth');
console.log(theme); // Output: true

// Removing data from session storage
sessionStorage.removeItem('auth');



Reffered: https://www.geeksforgeeks.org


JavaScript

Related
How to use the Arguments Object in a Function in JavaScript ? How to use the Arguments Object in a Function in JavaScript ?
JavaScript innerHTML JavaScript innerHTML
How to Replace an Object in an Array with Another Object Based on Property ? How to Replace an Object in an Array with Another Object Based on Property ?
Driver.js a JavaScript Framework Driver.js a JavaScript Framework
What are JavaScript Events ? What are JavaScript Events ?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
10