SessionStorage.getItem JavaScript Example

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

Syntax

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

sessionStorage.getItem() Method Example

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

The getItem() method allows you to access the data stored in the browser’s sessionStorage 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:
sessionStorage.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(sessionStorage.getItem('id'));

Comments