1

I'm currently testing on my local machine so the URL is as following when the user presses Sign out

// The ********** is my site name, which i don't want releasing yet
http://localhost:8080/**********/logout/

Local storage item:

Key: tkn Value: EE8D9DA0F1D71A0BE8B80F3BA80343555655871151tk16

the following tests i've tried:

if (localStorage.getItem('tkn')) {
    localStorage.clear();
} 

if (localStorage.getItem('tkn')) {
    localStorage.removeItem('tkn');
} 

// I've also ran these both tests in the console of the browser 
// and they return back 'undefined' but the item is cleared.

However when I sign out, the user session is removed completely fine. However the localStorage doesn't seem to be cleared.

Could someone shred some light on this for me?

Shiladitya
  • 12,003
  • 15
  • 25
  • 38
YaBCK
  • 2,949
  • 4
  • 32
  • 61
  • Possible duplicate of [How to delete a localStorage item when the browser window/tab is closed?](https://stackoverflow.com/questions/9943220/how-to-delete-a-localstorage-item-when-the-browser-window-tab-is-closed) – zatopek Nov 28 '17 at 14:36

1 Answers1

3

Instead of checking a specific property, check the length

if (localStorage.length > 0 ) {
    localStorage.clear();
} 

localStorage is an implementation of Storage interface and Storage has a property called length

Storage.length

Returns an integer representing the number of data items stored in the Storage object.

gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • 1
    @charlietfl In that case, specific items should be checked. But here OP has specifically asked *how to clear localstorage* – gurvinder372 Nov 28 '17 at 14:40