LocalStorage.key JavaScript Example

key(): Passed a number to retrieve nth key of a LocalStorage.

Syntax

Syntax for Key() Method:
var KeyName = localStorage.key(index);

localStorage.key() Method Example

The following function iterates over the local storage keys:
function forEachKey(callback) {
  for (var i = 0; i < localStorage.length; i++) {
    callback(localStorage.key(i));
  }
}
The following function iterates over the local storage keys and gets the value set for each key:
for(var i =0; i < localStorage.length; i++){
  console.log(localStorage.getItem(localStorage.key(i)));
}

Reference



Comments