0

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")); }
Babis.amas
  • 469
  • 2
  • 16
Mo7
  • 27
  • 5
  • 1
    _"I can't call (array.length)"_ Why not? _"can I do something like this?"_ Yes. – jabaa Dec 08 '21 at 11:28
  • 1
    @jabaa first time he runs the app the array is undefined. – Babis.amas Dec 08 '21 at 11:31
  • 1
    It's a common pattern to initialize a variable with `null`. `null` and `[]` have a different meaning. `null` means that data wasn't loaded. `[]` means that data was loaded but there is no data. – jabaa Dec 08 '21 at 11:32
  • 1
    Somehow that syntax looks terribly wrong. I think it's... – user202729 Dec 08 '21 at 11:38
  • 3
    [conditional - Javascript: If (x === 'foo' || 'bar') does not yield a true statement when i use 'bar' . Why? - Stack Overflow](https://stackoverflow.com/questions/53193769/javascript-if-x-foo-bar-does-not-yield-a-true-statement-when-i-use) – user202729 Dec 08 '21 at 11:39

1 Answers1

1
function checkemail(userArray, email) {

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

This might be working too:

userArray ??= [];
Babis.amas
  • 469
  • 2
  • 16