How to use localStorage with JavaScript ?

A short tutorial on how to use the localStorage with JavaScript using VueJS

Published 2021-07-16

localStorage with JavaScript

// To create or update a new item
localStorage.setItem('darkMode', 'false')

// To get an item
localStorage.getItem('darkMode') // 'false'

// To remove an item
localStorage.removeItem('darkMode') // 'darkMode' is now null

// Please note : localStorage ONLY use String!

localStorage.setItem('darkMode', false) 
// false will be converted to a String

console.log(localStorage.getItem('darkMode') ? 'Value is True' : 'Value is False') // 'Value is True'

console.log(localStorage.getItem('darkMode') === 'true' ? 'Value is True' : 'Value is False') // 'Value is False'

Related Tutorials :