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.

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

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

<!DOCTYPE html>
<html>
<head>
<title>Window localStorage Property</title>
</head>
<body>
<h2>Window localStorage Property</h2>
<div id="response"></div>

<script>
// Check browser support
if (typeof(Storage) !== "undefined") {
    // Store
    localStorage.setItem("lastname", "Jaydeep");
    // Retrieve
    document.getElementById("response").innerHTML = localStorage.getItem("lastname");
} else {
    document.getElementById("response").innerHTML = "Sorry, your browser does not support Web Storage...";
}
</script>
<a href="https://www.pakainfo.com/" target="_blank" alt="pakainfo" title="pakainfo">Free Download Example - Pakainfo.com</a>

</body>
</html>

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.


Related FAQ

Here are some more FAQ related to this Article:

  1.   Angularjs Login And Registration Modal Template
  2.   react open new tab with url Example Tutorial
  3.   Laravel Passing Multiple Variables from Controller to View
  4.   Wordpress $wpdb Insert Multiple Records
  5.   Array push key value pair Dynamically in javascript
  6.   Jquery Get Number of Characters String length
  7.   Filters Example (Number, Currency) using Angular
  8.   JavaScript Automatically Page Redirection
  9.   vuejs Radio Button on Form Submit Example
  10.   Jquery Count Child Elements with class

Leave a Reply

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