Search code examples
javascriptlocal-storageweb-storage

Check if exist any Key localStorage Javascript


I need to check if there is any key in localStorage This code did not work

    if (localStorage.user === undefined) {
      alert('There is no key!'); 
    }

Solution

  • if (Object.keys(localStorage).length === 0) {
      alert('There is no key!'); 
    }