• Home
  • Categories
  • Video Tutorials
    • Angular 5
  • News
  • About us
  • Contact us
  • Login
test
Code4Developers

Code4Developers

Code4Developers
  • Home
  • Categories
  • Video Tutorials
    • Angular 5
  • News
  • About us
  • Contact us
  • Login
  • JavaScript

Local Storage and Session Storage

  • Yatendrasinh Joddha
  • May 22, 2017
  • 2 minute read
javascript
Total
0
Shares
0
0
0

What is local Storage?

local storage is property for accessing Storage object, which is used to store and retrieve data from user’s browser. It is accessible only at client side not at server side like cookie.

Data stored in localStorage is accessible thought all pages under same domain, until user does not delete it manually. Even though user closes the browser, data will be accessible next time.

Syntax:

Set local storage:
localStorage.setItem('ProductName', 'Mobile');

localStrorage stores data in Key/Value format. Same syntax can be written as

localStorage.ProductName = 'Mobile';

Note: Here ProductName is key and Mobile is value for local storage. Key is case sensitive so here ProductName and productname will be considered as two different keys.

Get local storage:

var ProductName = localStorage.getItem('ProductName');

OR

var ProductName = localStorage.ProductName;

localStorage will always return value in string. So if required, then one need to cast the value in needed type.

Remove local storage:

localStorage.removeItem('ProductName');

This will remove ‘ProductName’ from local storage of under current domain.

localStorage.clear();

This will remove all the local storage for current domain.

What is session Storage?

Session storage is almost same as local storage. Only difference is, session storage will get cleared once user will close the browser window.

Syntax:

All the example written for localStorage can be used for sessionStorage as below

Set session storage:

sessionStorage.setItem('ProductName', 'Mobile');

OR

sessionStorage.ProductName = 'Mobile';

Get session storage:

var ProductName = sessionStorage.getItem('ProductName');

OR

var ProductName = sessionStorage.ProductName;

Remove session storage:

sessionStorage.removeItem('ProductName');

sessionStorage.clear();

Browser Support for Local Storage and Session Storage:

Browser Version
Chrome 4.0
Firefox 3.5
Internet Explorer 8.0
Opera 10.50
Safari 4.0

Storage Capacity:

Storage capacity for localStorage and sessionStorage is vary based on browser. Generally browsers provides around 5MB of storage space.

Click Here to read more JavaScript articles on Code4Developers. If you are getting started with Angular then I recommend this official documentation to refer.

Yatendrasinh Joddha
Yatendrasinh Joddha

I have healthy knowledge and experience in Azure, O365 and ASP.Net with c#, MVC, WebApi along with client-side frameworks and libraries like JavaScript, JQuery, KnockoutJs, AngularJs, Angular, ReactJs, NodeJs

Views: 23,579

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on WhatsApp (Opens in new window) WhatsApp
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on X (Opens in new window) X
  • Click to share on Pinterest (Opens in new window) Pinterest
  • Click to email a link to a friend (Opens in new window) Email
  • Click to print (Opens in new window) Print

Like this:

Like Loading...

Related Posts

Total
0
Shares
Share 0
Tweet 0
Pin it 0
3 comments
  1. Anonymous says:
    January 18, 2019 at 4:11 pm

    Nice, worked for me thanks

    Reply
  2. Anonymous says:
    February 15, 2019 at 11:25 am

    Code is fine but please provide more info for beginners

    Reply
  3. Anonymous says:
    August 29, 2019 at 1:23 pm

    Can you please share knowledge about how to use an object for localstorage in angular 7

    Reply

Leave a ReplyCancel reply

Subscribe to Website via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Recent Posts
  • Getting Started with AWS Step Functions: Orchestration Made Simple

    Getting Started with AWS Step Functions: Orchestration Made Simple

    4 weeks ago
  • React Hooks Guide: Top Tips for Optimizing Performance in Your React Applications

    React Hooks Guide: Top Tips for Optimizing Performance in Your React Applications

    1 year ago
  • Demystifying JavaScript Tree Shaking: Boosting Performance and Reducing Bundle Size

    Demystifying JavaScript Tree Shaking: Boosting Performance and Reducing Bundle Size

    2 years ago
  • Unlocking the Power of React Hooks: A Comprehensive Guide with Examples

    Unlocking the Power of React Hooks: A Comprehensive Guide with Examples

    2 years ago
  • Celebrating a Decade of Phenomenal Growth: Insights and Reflections on 10 Years of Software Engineering

    Celebrating a Decade of Phenomenal Growth: Insights and Reflections on 10 Years of Software Engineering

    2 years ago
  • Angular Custom Elements: Creating Reusable Components with Angular

    Angular Custom Elements: Creating Reusable Components with Angular

    2 years ago
  • Connect Firebase Realtime NoSQL Database with Angular App from Scratch

    Connect Firebase Realtime NoSQL Database with Angular App from Scratch

    5 years ago
  • How to Build an Inclusive Esports Community

    How to Build an Inclusive Esports Community

    5 years ago
  • Best Digital Icebreakers

    Best Digital Icebreakers

    5 years ago
  • Email alerts when a docker container stopped in AWS ECS CLUSTER

    Email alerts when a docker container stopped in AWS ECS CLUSTER

    5 years ago
Subscribe to Website via Email

Enter your email address to subscribe to this website and receive notifications of new posts by email.

Featured Posts
  • javascript 1
    Spread syntax (three dots) in JavaScript
    • March 21, 2018
  • Angular 2
    Angular 6 CRUD – Part 1: Project Setup, Routing, Service
    • May 9, 2018
  • javascript 3
    Local Storage and Session Storage
    • May 22, 2017
  • Angular 4
    Angular 4 Project Structure
    • June 18, 2017
  • AWS 5
    Email alerts when a docker container stopped in AWS ECS CLUSTER
    • July 24, 2020
Code4Developers
Learning is never ending process

Input your search keywords and press Enter.

%d