1localStorage.setItem('myCat', 'Tom');
2
3var cat = localStorage.getItem('myCat');
4
5localStorage.removeItem('myCat');
6
7// Clear all items
8localStorage.clear();
9
1function createItem() {
2 localStorage.setItem('nameOfItem', 'value');
3}
4createItem() // Creates a item named 'nameOfItem' and stores a value of 'value'
5
6function getValue() {
7 return localStorage.getItem('nameOfItem');
8} // Gets the value of 'nameOfItem' and returns it
9console.log(getValue()); //'value';
1var answer = localStorage.key(1);
2// this statement will retrieve the value of the second item in localStorage.
1<form action="2.html" onsubmit="callme()">
2 <p>Enter your name</p>
3 <input id="tbName" type="text">
4 <button type="submit" value="submit">Submit</button>
5</form>
6<script>
7 function callme(){
8 var name = document.getElementById('tbName').value;
9 sessionStorage.setItem('userName', name);
10 }
11</script>
12
1window.localStorage.setItem("grade","One");
2//in this case, the `grade` is the key while `One` is the value.