Skip to content
Share
Explore

Data Storage JSON

Local Storage

localStorage.setItem ('key', 'value')
localStorage.getItem ('key')
localStorage.removeItem ('key')
localStorage.clear()
Check in Chrome / Developer / Application / Storage / Local Storage
Local Storage = Local Browser

Local Storage ONLY SUPPORT STRINGS

This will render UNDEFINED
const users = [
{
username: 'nghi',
age: 40
},
{
username: 'thao',
age: 41
}
]

localStorage.setItem('listUsers',users)

const getNghi = localStorage.getItem('listUsers')
console.log(getNghi[0].age)

Convert Array / Objects to Strings

const usersJSON = JSON.stringify(users)
localStorage.setItem('listUsers', usersJSON)
Note: Key in JSON user double-quote, not single-quote

Convert Strings back to Objects / Arrays

const getUserJSON = localStorage.getItem('listUsers')
const getUser = JSON.parse(getUserJSON)

Want to print your doc?
This is not the way.
Try clicking the ··· in the right corner or using a keyboard shortcut (
CtrlP
) instead.