LocalStorage.getItem JavaScript Example

LocalStorage.getItem() Method: Retrieve a value by the key from LocalStorage.

Syntax

window.localStorage
The syntax for SAVING data to localStorage:
localStorage.setItem("key", "value");
The syntax for READING data from localStorage:
var lastname = localStorage.getItem("key");

localStorage.getItem() Method Example

Check out LocalStorage.setItem JavaScript Example to add key and value to LocalStorage.

The getItem() method allows you to access the data stored in the browser’s localStorage object.
It accepts only one parameter which is the key and returns the value as a string.
To retrieve the user object from the key stored above:
localStorage.getItem('id');
This returns a string with value as;
"{"firstName":"Ramesh","lastName":"Fadatare"}"
To use this value, you would have to convert it back to an object using JSON.parse() method.
JSON.parse(localStorage.getItem('id'));


Comments