JavaScript Web Storage

Cookies, Local Storage and Session Storage in JavaScript

Posted by

Cookies, Local Storage, and Session Storage are web storage in which web application stores the data on the user’s browser. Also, we can say that we can use Cookies, Local Storage, and Session Storage to store the data on the client side.

Before HTML5, we had used Cookie to store data on the client side. Web storage, such as localStorage and sessionStorage, has been introduced in HTML5 and is now easier to use.

Cookies

We set Cookie Name, value, expiry date, and the path to set the cookie in JavaScript. We use “document.cookie” to create, edit and delete the Cookie in JavaScript.

Syntax:

These are the features of cookies:

  1. Has only 4kb of holding capacity
  2. Supported in both HTML4 and HTML5
  3. Accessible from any window inside the browser
  4. The expiration date can be manually set
  5. Can be stored on both browser and server
  6. Can be sent with API Requests

Local Storage:

Local storage stores the data on the user’s browser. It stores data in key/value pairs. Data is not removed even on browser close and tab change.

Storing data in Local Storage:

We use the following syntax to set the data.

Syntax:

Example:

The “userName” and its value will be set in the Localstorage of the browser.

Retrieving data from Local Storage:

We use the following syntax to retrieve the data:

Syntax:

Example to retrieve the local storage value:

We can also store JSON objects in Local Storage.

Example:

Removing the data from Local Storage:

We use the following syntax to remove the data from Local Storage:

Syntax:

Example:

Also, we can remove all Local Storage data from the browser by the below syntax:

These are the features of Local Storage:

  1. Has 10MB of holding capacity
  2. Supported in HTML5
  3. Accessible from any window inside the browser
  4. No expiration date
  5. It stores on a browser only
  6. Cannot be sent with API Requests

Session Storage:

Session storage stores the data on the user’s browser. Session Storage provides the same properties and methods as Local storage. The difference is that it expires on browser closed but not expiration data of Local Storage.

The syntax for storing data in sessionStorage is as follows:

The syntax for retrieving data from sessionStorage is as follows:

The syntax for removing the saved data from sessionStorage is as follows:

The syntax for removing all saved data from sessionStorage is as follows:

These are the features of Session Storage:

  1. Has 5MB of holding capacity
  2. Supported in HTML5
  3. Accessible in the same tab inside the browser
  4. Expires on tab close
  5. It stores on a browser only
  6. Can not be sent with API Requests

Difference between Cookies, Local Storage, and Session Storage

Cookies Local Storage Session Storage
Maximum Storage 4kb 10MB 5MB
Supported in HTML5 Supported in both HTML4 and HTML5 Supported in HTML5 Supported in HTML5
Accessible from any window inside the browser Accessible from any window inside the browser Accessible from any window inside the browser Accessible in the same tab inside the browser
Expiration date The expiration date can be manually set No expiration date Expires on tab close
Stored on Cookies store on both browser and server Local Storage stores on a browser only Session Storage stores on a browser only
Can be sent with API Requests Yes No No

Leave a Reply

Your email address will not be published. Required fields are marked *