SessionStorage.clear JavaScript Example

SessionStorage.clear() : clears all keys stored in a given SessionStorage object.

This method, when invoked clears the entire storage of all records for that domain. It does not receive any parameters.

Syntax

// Save data to sessionStorage
sessionStorage.setItem('key', 'value');

// Get saved data from sessionStorage
let data = sessionStorage.getItem('key');

// Remove saved data from sessionStorage
sessionStorage.removeItem('key');

// Remove all saved data from sessionStorage
sessionStorage.clear();

localStorage.clear() Method Example

The following function creates three data entries in local storage and then deletes them by using clear().
function populateStorage() {
  sessionStorage.setItem('bgcolor', 'red');
  sessionStorage.setItem('font', 'Helvetica');
  sessionStorage.setItem('image', 'miGato.png');

  sessionStorage.clear();
}

Reference

Comments