Posted inTechnology / JavaScript / jQuery

Simple LocalStorage SessionStorage using JavaScript

Today, We want to share with you Simple LocalStorage SessionStorage using JavaScript.In this post we will show you localstorage expiration, hear for how to store data in localstorage in javascript we will give you demo and example for implement.In this post, we will learn about with an example.

Simple LocalStorage SessionStorage using JavaScript

There are the Following The simple About Simple LocalStorage SessionStorage using JavaScript Full Information With Example and source code.

As I will cover this Post with live Working example to develop session-storage vs local-storage, so the local storage javascript for this example is following below.

LocalStorage Introduction

  • setItem(): javascript simple Add key and value to local storage
  • getItem() : javascript get all a value by the key
  • removeItem() : delete or remove an item by key using javascript
  • clear() : (reset)Clear all storage in javascript

Local-storage Syntax

localStorage.setItem('key', 'value');
localStorage.getItem('key');
localStorage.removeItem('key');
localStorage.clear();

Checking for Browser Support

if (localStorage) {
  // LocalStorage is supported!
} else {
  // No LocalStorage support. 
}
//Storing Data in LocalStorage
localStorage.setItem('product_name', 'Iphone 8');

// Functions
localStorage.setItem('product_name', 'Iphone 8');

// Object
localStorage.product_name = 'Iphone 8';

// Array
localStorage['product_name'] = 'Iphone 8';

//get all Data from LocalStorage
var product_name = localStorage.getItem('product_name');

//Removing Data from LocalStorage
localStorage.removeItem('product_name');

//Retrieving Keys
for (var i = 0; i < localStorage.length; i++) {
  console.log(localStorage.key(i))
};

JavaScript SessionStorage

// JavaScript Storing Data
sessionStorage.setItem('product_name', 'Iphone 8');

// JavaScript get all the Data
var product_name = sessionStorage.getItem('product_name');

// Deleting Data in JavaScript
sessionStorage.removeItem('product_name');

// get all an Item Key
sessionStorage.key(n);

// Clearing the Datastore using JavaScript
sessionStorage.clear();

Storing Objects in JavaScript localStorage

var productObj = { 'iteam1': 'mobile', 'iteam2': 'laptop', 'iteam3': 'bags' };

// Put the object into storage
localStorage.setItem('productObj', JSON.stringify(productObj));

// get all the object from storage
var all_product_objects = localStorage.getItem('productObj');

console.log('all_product_objects: ', JSON.parse(all_product_objects));

Simple LocalStorage SessionStorage using JavaScript




Window localStorage Property


Window localStorage Property

Free Download Example - Pakainfo.com
Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Simple LocalStorage SessionStorage using JavaScript.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

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

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype