We’ll cover how to use local storage in three ways:
- Basic local storage usage
- How to store multiple values in local storage
- How to use local storage with a library
Basic local storage usage
You can save things into local storage with setItem.
Since local storage only supports string values, the values you saved inside local storage will be converted into a string.
localStorage.setItem('key', 'value')You can get the item back from local storage with getItem.
localStorage.getItem('key')To delete items from local storage, you use deleteItem.
local storage.deleteItem('key')How to store multiple values in local storage
You can save multiple values in local storage using two methods:
- Using multiple entries
- Save the values as a JSON Object.
Using multiple entries
Using multiple entries is straightforward — you save each value as a separate entry.
You can do this by calling setItem for each value you want to save.
// Using multiple entrieslocal storage.setItem('access_token', access_token)local storage.setItem('expiresAt', Date.now() + expires_in * 1000)Using a JSON Object
It’s also quite straightforward to save values as a JSON Object. To do this, you convert the JavaScript object (or array) into JSON with JSON.stringify.
// Saving the values as a JSON objectconst token = { value, expiresAt: Date.now() + expires_in * 1000,}
local storage.setItem('token', JSON.stringify(token))If you want to get the object value back from local storage, you have to use getItem to retrieve the JSON value, then JSON.parse to convert it back to an object.
const token = JSON.parse(local storage.getItem('token'))Making things easier with a library
I created a library called localStore that helps you make local storage easier to use. You can read the documentation here.
It helps you save strings and objects without worrying about the JSON conversion — it does the JSON conversion for you automatically (and when necessary).
You can install localStore by installing with this command:
npm install @zellwk/javascriptYou can include localStore in your project like this:
import localStore from '@zellwk/javascript/browser/localStore'If you don’t use npm, you can include localStore in your project like this:
import localStore from 'https://cdn.jsdelivr.net/npm/@zellwk/[email protected]/src/browser/localstore.js'Remember to change the version number from @3.0.0 to the version you’re
requesting for! See releases
for the latest release.
Using localStore
When you save a value, localStore converts objects into JSON automatically so you don’t have to write JSON.stringify.
// Saving a value into local storageconst value = { access_token, expiry: Date.now() + expires_in * 1000,}
localStore.set('token', value)When you retrieve a value, localStore converts the value from JSON back to JavaScript automatically, so there’s no need to write JSON.parse.
// Getting an item from local storageimport localStore from '@zellwk/javascript/localstore'
const token = localStore.get('token')Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson