Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

200
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!