We use the sessionStorage
object to save the key-value pairs for a specific session. The key and value should be strings. The value will only be available until the browser tab is open. If the user closes the tab, the data will be cleared.
Here are the available methods:
setItem(key, value)
: This adds key-value pair to sessionStorage
.getItem(key)
: This gets the value of the key.removeItem(key)
: This removes the key-value pair.clear()
: This deletes all key-value pairs.key(index)
: This gets the key number index.length
: This is the number of key-value pairs present.The code below demonstrates how to use sessionStorage
:
setItem()
method of the sessionStorage
object to add two entries to the object.getItem()
method of the sessionStorage
object to get the value associated with the name
key.removeItem()
method of the sessionStorage
object to remove the entry with the name
key.clear()
method of the sessionStorage
object to remove all data.