mercredi 5 février 2020

With an If statement, how can I skip a variable being checked if its property could be undefined?

I would like to be able to skip checking a variable if the property (.emailAddress) has the chance of being undefined but continue checking the others.

For example, I'm checking an entered email address if its an existing contact (email) on the page.

if(inputEmail.length > 0 && inputEmail.indexOf('@') > 0 */ check that the input is not blank & is probably an email address.

  && inputEmail !== existingContact1.emailAddress
  && inputEmail !== existingContact2.emailAddress
  && inputEmail !== existingContact3.emailAddress
  && inputEmail !== existingContact3.emailAddress
)   {
// execute code
}

My problem occurs if .emailAddress is undefined, as you can't check undefined in an If statement. .emailAddress could be any combination of existingContact 1-4 that could be undefined or not.

I've tried using typeof to find if it is undeclared with not the results I was expecting.

&& typeof existingContact1 == "undefined" || typeof existingContact1.emailAddress == "undefined" && existingContact1.emailAddress !== inputEmail

What are some diffrent approaches to be able to anticpte and skip over if .emailAddress has the chance of being undefined?

Aucun commentaire:

Enregistrer un commentaire