What is LocalStorage

1. Definition

The localStorage read-only property of the window interface allows you to access a Storage object for the Document’s origin; the stored data is saved across browser sessions.

localStorage is similar to sessionStorage, except t…


This content originally appeared on DEV Community and was authored by Abdullah Furkan Özbek

1. Definition

The localStorage read-only property of the window interface allows you to access a Storage object for the Document's origin; the stored data is saved across browser sessions.

localStorage is similar to sessionStorage, except that while localStorage data has no expiration time, sessionStorage data gets cleared when the page session ends — that is, when the page is closed.

localStorage data for a document loaded in a "private browsing" or "incognito" session is cleared when the last "private" tab is closed.

2. Data Format

The keys and the values stored with localStorage are always in the UTF-16 DOMString format, which uses two bytes per character. As with objects, integer keys are automatically converted to strings.

3. Example

// Setting and item: key: value
localStorage.setItem('myCat', 'Tom');

// Reading an item
const cat = localStorage.getItem('myCat');

// Remove an item
localStorage.removeItem('myCat');

// Clear all items
localStorage.clear();

4. Advaced Examples

For setting objects or dates we need to convert to strings because localStorage only store string format

let userList = [{name: "David"}, {name: "Kevin"}]
let date = new Date()

// Setting
localStorage.setItem("userList", JSON.stringify(userList))
localStorage.setItem("date", date.toString())

// Reading
userList = JSON.parse(localStorage.getItem("userList"))
date = new Date(localStorage.getItem("date"))

Screenshot of localstorage

 Links


This content originally appeared on DEV Community and was authored by Abdullah Furkan Özbek


Print Share Comment Cite Upload Translate Updates
APA

Abdullah Furkan Özbek | Sciencx (2021-09-06T07:18:35+00:00) What is LocalStorage. Retrieved from https://www.scien.cx/2021/09/06/what-is-localstorage-3/

MLA
" » What is LocalStorage." Abdullah Furkan Özbek | Sciencx - Monday September 6, 2021, https://www.scien.cx/2021/09/06/what-is-localstorage-3/
HARVARD
Abdullah Furkan Özbek | Sciencx Monday September 6, 2021 » What is LocalStorage., viewed ,<https://www.scien.cx/2021/09/06/what-is-localstorage-3/>
VANCOUVER
Abdullah Furkan Özbek | Sciencx - » What is LocalStorage. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/06/what-is-localstorage-3/
CHICAGO
" » What is LocalStorage." Abdullah Furkan Özbek | Sciencx - Accessed . https://www.scien.cx/2021/09/06/what-is-localstorage-3/
IEEE
" » What is LocalStorage." Abdullah Furkan Özbek | Sciencx [Online]. Available: https://www.scien.cx/2021/09/06/what-is-localstorage-3/. [Accessed: ]
rf:citation
» What is LocalStorage | Abdullah Furkan Özbek | Sciencx | https://www.scien.cx/2021/09/06/what-is-localstorage-3/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.