Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

112
Visualizações
Null object is String type in localStorage

I am coming from Java dev and new to javascript, can someone explains what is going on.

I am using localStorage to store my token in my browser.

localStorage.token = 'xxx'

When the users sign out, we remove the token, here is the code

localStorage.token = null

If we want to check wether the user is authenticated or not, we check the token

const isAuth = localStorage.token !== null

Here is the thing becomes weird to me. After I marked localStorage.token to null,

the result of localStorage.token !== null is still true !


I had done some investigation,

I logged the localStorage object and found out the token variable is "null" instead of null.

Here is my assumption.

When the browser needs to store the localStorage, it iterates inside the object fields, after finding the null object, it use toString to store the final form?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

give a value or overwrite the one present in the localstorage with the function set item, like code below :

localStorage.setItem('token' , 'your token here ');

and for access the value :

localStorage.getItem('token')
window.console.log("token :",localStorage.getItem('token'))
about 4 years ago · Juan Pablo Isaza Relatório

0

Localstorage in the browsers work only on the string types

localStorage.getItem('someItem');
localStorage.setItem('someItem', 'string value');
localStorage.removeItem('someItem');
about 4 years ago · Juan Pablo Isaza Relatório

0

Use localStorage methods for setting, getting and removing token.

Check this link for more details: https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

And for checking weather the token exists or not you can do this:

const isAuth = !!localStorage.token
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda