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
Also Read :  PHP Laravel Eloquent Increments and Decrements value

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://pakainfo.com/" target="_blank" alt="pakainfo" title="pakainfo">Free Download Example - Pakainfo.com</a>

</body>
</html>

Angular 6 CRUD Operations Application Tutorials

Read :

Also Read :  C# Named and Optional Arguments Tutorial with Examples

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.

Recommended For You.

Laravel 5.7 Stripe Payment Gateway Example From Scratch
Today, We want to share with you Laravel 5.7 Stripe Payment Gateway Example From Scratch.In this post we will show

Add a Comment

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