Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting from localStorage: should I use delete or .removeItem?

I'm using localStorage in a Chrome extension.

Is there any difference between delete localStorage[key] and window.localStorage.removeItem(key) or do they behave identically?

Note that I choose window.localStorage.removeItem(key) to correspond exactly to the spec, but localStorage.removeItem(key) is how it would probably look in the wild.

like image 966
Alex Churchill Avatar asked Aug 02 '11 01:08

Alex Churchill


People also ask

What is difference between localStorage removeItem and localStorage clear?

getItem() : This is how you get items from localStorage. removeItem() : Remove an item by key from localStorage. clear() : Clear all localStorage. key() : Passed a number to retrieve the key of a localStorage.

Should I delete local storage?

User agents should expire data from the local storage areas only for security reasons or when requested to do so by the user. User agents should always avoid deleting data while a script that could access that data is running.


1 Answers

There's no difference in Chrome, but i recommend using localStorage.removeItem(key) so that you get in the habit of doing so. That way, when you go to use localStorage in a website, you are already using the method that is most likely to work with shims and polyfills for outdated browsers.

like image 139
Nathan Bubna Avatar answered Sep 30 '22 14:09

Nathan Bubna