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

171
Vistas
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 Respuestas
Responde la pregunta

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 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