Local Storage and Session Storage (JavaScript)
Tags: react, webdev, javascript, beginners
Published: 2022-03-01 17:20:09

No, I won't tell you any definition. Just get on the train and by the end of this blog, you'll have a clear understanding of local storage and session storage.

1. Inspect whatever page you are on.
2. Click on Application.
3. Click on storage and you'll see local storage and session storage right there.
![1.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1646146100856/MdCX00JfR.PNG)
Both local storage and session storage store key-value pairs.

The major difference between local storage and session storage is that after closing the browser the key-value pairs stored in session storage get lost.

Now, let's understand how to operate on local storage with some examples.

Example1: give key-value pair to the local storage.
```javascript
localStorage.setItem(Name1, 'Rajat');
```
![2.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1646146608205/LGqLGpb-k.PNG)

Let's see "typeof" of the local storage:
![3.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1646146882080/luQz9M95N.PNG)

In the above example, we saw how to set key-value pairs in the local storage. Now, let's understand how to get items from the local storage.<br/>
Example 2:
```javascript
let name = localStorage.getItem('Name1')
console.log(name)
```
```javascript
Result: Rajat
```

Now, let's try to get some value that did not exist.<br/>
Example3:
```javascript
let name = localStorage.getItem('Name2')
console.log(name)
```
```javascript
Result: null
```
The result is null if you try to fetch something from local storage that did not exist.

Many times we want to store arrays in local storage since arrays are easy to work with (we have so many inbuilt methods for arrays). However, one limitation of local storage is that it stores the array as a string. let's see what I mean:
```javascript
  localStorage.setItem('Name1', 'Rajat');

  let fruitsArray = ['Apple', 'Banana', 'Orange', 'Grapes', 'Mango']
  localStorage.setItem('impArray', fruitsArray);
```
![4.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1646147741775/bkjZ3N5wY.PNG)
In order to overcome this issue ☝️, we use JSON.stringify. See it in action below:<br/>
Example 4: storing array in local storage:
```javascript
  localStorage.setItem('Name1', 'Rajat');

  let fruitsArray = ['Apple', 'Banana', 'Orange', 'Grapes', 'Mango']
  localStorage.setItem('impArray', JSON.stringify(fruitsArray));
```
![5.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1646147964542/8zzxWLmBd.PNG)

Example 5: getting array from local storage.
```javascript
 console.log(localStorage.getItem('impArray'));
```
![6.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1646148123116/6ENg4SxVj.PNG)
The above result may look like an array but we are getting string from local storage instead of array. let me prove it.
```javascript
 console.log(typeof localStorage.getItem('impArray'));
```
![7.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1646148254268/3orQydKxV.PNG)
Therefore, in order to get an array from local storage, we use JSON.parse. See below.<br/>
Example 6: get an array from the local storage?
```javascript
 console.log(JSON.parse(localStorage.getItem('impArray')));
```
Hence, we use:<br/>
1. JSON.stringify: To set array as the value in local storage.
2. JSON.parse: To get an array from local storage.

Example 7: Clear local storage.
```javascript
  localStorage.setItem('Name1', 'Rajat');

  let fruitsArray = ['Apple', 'Banana', 'Orange', 'Grapes', 'Mango']
  localStorage.setItem('impArray', JSON.stringify(fruitsArray));

  localStorage.clear()
```
![8.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1646152924097/cKjtLArDN.PNG)

Hence, we can clear localStorage using ```localStorage.clear()```.<br/>

Example 8: Remove only the 'name1' key-value pair from local storage.
```javascript
  localStorage.setItem('Name1', 'Rajat');

  let fruitsArray = ['Apple', 'Banana', 'Orange', 'Grapes', 'Mango']
  localStorage.setItem('impArray', JSON.stringify(fruitsArray));

  localStorage.removeItem('Name1');
```
![9.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1646153259332/qlwi3O0QQ.PNG)
As you can see above, In order to remove a particular key-value pair we use ```localStorage.removeItem```. 

That's all about local storage. <br/>
We run similar operations in session storage, the only difference being we use session storage in place of local storage:
```
1. sessionStorage.setItem(Name1, 'Rajat');
2. sessionStorage.getItem(Name1, 'Rajat');
3. sessionStorage.setItem('impArray', JSON.stringify(fruitsArray));
4. console.log(JSON.parse(sessionStorage.getItem('impArray')));
5. sessionStorage.clear()
6. sessionStorage.removeItem('Name1');
```
Let me reiterate: The only major difference between local storage and session storage is as soon as we close the browser, we lose whatever is saved in session storage. However, this is not the case with local storage.

and finally, let's see what MDN has to say:

The localStorage read-only property of the window interface allows you to access a Storage object for the Document's origin; the stored data is saved across browser sessions.<br/>
The read-only sessionStorage property accesses a session Storage object for the current origin. sessionStorage is similar to localStorage; the difference is that while data in localStorage doesn't expire, data in sessionStorage is cleared when the page session ends.

That's all folks.

**If you have any doubt ask me in the comments section and I'll try to answer as soon as possible.**

**I write one article every day related to web development (yes, every single day). Follow me here if you are learning the same..**

**If you love the article follow me on Twitter:** @therajatg

**If you are the Linkedin type, let's connect**: https://www.linkedin.com/in/therajatg/

**Have an awesome day ahead 😀!**