检查是否存在任何 Key localStorage Javascript

Check if exist any Key localStorage Javascript

我需要检查 localStorage 中是否有任何密钥 此代码无效

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

您需要使用getItem

if (localStorage.getItem("user") === null) {
    alert('Não há chave!'); 
}

编辑 您可以通过

检查localstorage是否为空
if (localStorage.length == 0)
if (Object.keys(localStorage).length === 0) {
  alert('There is no key!'); 
}