Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

202
Vistas
Why JavaScript null check doesn't working?
function readProperty(property)
{
  console.log(localStorage[property]) //Alerts “null”
  if(localStorage[property] == null)
  {
      console.log('Null chek')
      return false;
  }

  return localStorage[property];
}

log outputs "null", but 'if()' doesn't work. I try with ===, its not work too. Help please.

UPD: Thanks everyone this change helped me if(localStorage[property] == 'null')

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

The keys and the values stored with localStorage are always in the UTF-16 string format, which uses two bytes per character. As with objects, integer keys are automatically converted to strings.

https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

Try:

localStorage[property] === 'null'
about 4 years ago · Juan Pablo Isaza Denunciar

0

Although: console.log(localStorage[property]) may report null, the actual value is undefined.

So, in your if statement, if you test against undefined, you'll get a match.

Better yet though, just test for the existence or non existence of a value with:

 if(localStorage[property])...  // Tests for any "truthy" value

or

 if(!localStorage[property])...  // Tests for any "falsey" value
about 4 years ago · Juan Pablo Isaza Denunciar

0

Well, I don't know if you're trying to use the global localStorage or if it's a defined variable in your code.

If you're using the localStorage API, you should check if a key exists like this...

if (!localStorage.getItem("my-item")) {
  console.log("item doesn't exist.");
}

The .getItem() method returns null when the key isn't defined so checking using ! or item !== null work.

.getItem() reference from MDN, https://developer.mozilla.org/en-US/docs/Web/API/Storage/getItem.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda