Loading Please Wait...

Logo Lynxsia IT Solutions

JavaScript Storage API

JS Web Storage API

The Web Storage API is a simple syntax for storing and retrieving data in the browser. It is very easy to use.

Type of Storage

JavaScript provides two type of storage API.

  • localStorage: Stores the data with no expiration date
  • sessionStorage: Stores the data for one session
Storage Object Properties and Methods

Both localStorage and sessionStorage have the following methods and properties:

Property/Method Description
key(n) Returns the name of the nth key in the storage
length Returns the number of data items stored in the Storage object
getItem(keyname) Returns the value of the specified key name
setItem(keyname) Adds a key to the storage, or updates a key value (if it already exists)
removeItem(keyname) Removes that key from the storage
clear() Empty all key out of the storage
The localStorage Object

The localStorage object provides access to a local storage for a particular Web Site. It allows you to store, read, add, modify, and delete data items for that domain.

The data is stored with no expiration date, and will not be deleted when the browser is closed.

The data will be available for days, weeks, and years unless the cache is cleared.

The sessionStorage Object

The sessionStorage object is identical to the localStorage object.

The difference is that the sessionStorage object stores data for one session.

The data is deleted when the browser is closed.

The setItem() Method

The setItem() method stores a data item in a storage.

It takes a key name and a value as parameters.

					 
        
          localStorage.setItem("company", "Lynxsia IT Solutions");

          // OR

          sessionStorage.setItem("company", "Lynxsia IT Solutions");
        
      
The getItem() Method

The getItem() method retrieves a data item from the storage.

It takes a key name as parameter.

					 
        
          localStorage.getItem("company");

          // OR

          sessionStorage.getItem("company");
        
      
The key() Method

The key() method returns name of the key with the specified index.

					 
        
          alert(localStorage.key(0));

          // OR

          alert(sessionStorage.key(0));
        
      
The removeItem() Method

The removeItem() method removes the specified Storage Object item.

					 
        
          localStorage.removeItem("company");

          // OR

          sessionStorage.removeItem("company");
        
      
The clear() Method

The clear() method removes all the Storage Object item.

					 
        
          localStorage.clear();

          // OR

          sessionStorage.clear();
        
      
The length Property

The length property returns the number of items stored in the browsers Storage Object.

					 
        
          alert(localStorage.length);

          // OR

          alert(sessionStorage.length);
        
      

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved