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

169
Views
Array in localStorage is not initialized at first run

window.reload = () => {
  var userArray = JSON.parse(localStorage.getItem("key"));
}

let feedback = document.getElementById("feedback");






function checkemail(userArray, email) {
  var i;
  if (userArray == null | undefined) {
    userArray = JSON.parse(localStorage.getItem("key"));
  }

  var person = {
    name: document.getElementById("nameinput").value,
    email: document.getElementById("emailinput").value,
    passowrd: document.getElementById("passwordinput").value
  };

  let isFound = false;

  for (i = 0; i < userArray.length; i++) { //here is the error it still happen even after I added the if null part 
    if (userArray != undefined)
      var oldemail = userArray[i].email;
    let newemail = document.getElementById("emailinput").value;
    if (newemail === oldemail) {
      isFound = true;
      i = userArray.length;

      return feedback.innerHTML = "email exist please log in or register with different email";
    }
  }

  if (!isFound) {
    return storeName(person, userArray);
  }
}


function storeName(person, userArray) {
  if (userArray != undefined)
    var person = {
      name: document.getElementById("nameinput").value,
      email: document.getElementById("emailinput").value,
      passowrd: document.getElementById("passwordinput").value
    };

  userArray = JSON.parse(localStorage.getItem("key"));

  userArray.push(person);
  userArray = JSON.stringify(userArray);
  localStorage.setItem("key", userArray);
  console.log(userArray);
}

I want to store an array in local storage, the first time when I run the code, of course, the array is empty and I can not use a loop for example because I can't call (array.length). so can I tell the compiler for example if the array is null or undefined just put length is zero or assign the value of the array to an empty array?

can I do something like this?

if( userArray == null | undefined) { userArray = JSON.parse(localStorage.getItem("key")); }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

function checkemail(userArray, email) {

  if (userArray == null || typeof(userArray) == 'undefined') {
    userArray = [];
  }
  // rest of the code
}

This might be working too:

userArray ??= [];
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!