/    Sign up×
Articles /Pin to ProfileBookmark

What LocalStorage is in JavaScript and how to use it

What is LocalStorage in Javascript and how do you use it?

In this article:
You will learn what LocalStorage is an how to use it. Let’s start!

Local storage is a feature of web storage that allows you to store data locally in the browser, even after the browser window has been closed. This data is stored in key-value pairs, with the key being a string that you define and the value being the data you want to store.

To use local storage in JavaScript, you can use the `localStorage` object, which is part of the window object. Here’s how you can use it:

  1. Set an item in local storage: 
    `localStorage.setItem(“youritem”, “value”);`
  2. Get an item from local storage:
    `var data = localStorage.getItem(“youritem”);`
  3. Remove an item from local storage:
    `localStorage.removeItem(“youritem”);`
  4. Remove all items from local storage:
    `localStorage.clear();`

It’s important to note that local storage has some limitations, such as the maximum size of data that can be stored, which varies from browser to browser. Also, local storage data is accessible to any script on your site, so it’s not a secure way to store sensitive information.

To work around these limitations, you can use session storage instead, which works in the same way as local storage but the data is deleted when the user closes the browser window. To use session storage, you can use the `sessionStorage` object instead of the `localStorage` object.

With these basic operations, you can easily use local storage in your JavaScript projects to store data that needs to persist even after the user has closed the browser window.

Back-endCSSFront-endHTMLJavaScriptWeb Hosting
×

Success!

Help @1 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 2.22,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Samric24,
tipped: article
amount: 1000 SATS,

tipper: Anonymous,
tipped: article
amount: 10 SATS,

tipper: @neststayhome,
tipped: article
amount: 1000 SATS,
)...