TheAlgorithms/JavaScript · warning · TypeError

Email Address String Null or Empty.

Error message

Email Address String Null or Empty.

What it means

Thrown by validateEmail() in String/ValidateEmail.js when str is exactly '' or exactly null. A TypeError raised before the regex test. Note the narrow guard: only '' and null are rejected — undefined, numbers, booleans, and other non-strings are NOT caught here and will instead fall through to the regex .test(), which coerces them to a string (so validateEmail(undefined) returns true because /undefined/.test matches).

Source

Thrown at String/ValidateEmail.js:6

/**
 * Returns whether the given string is a valid email address or not.
 */
const validateEmail = (str) => {
  if (str === '' || str === null) {
    throw new TypeError('Email Address String Null or Empty.')
  }

  return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(str)
}

export { validateEmail }

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Check for empty/null before calling, and also enforce typeof for safety: if (typeof str !== 'string' || !str.trim()) reject.
  2. Normalize null/undefined upstream so an absent email never reaches validation.
  3. Use a stricter validator that type-checks first, since this guard misses undefined and non-strings.
  4. Treat null as 'no email provided’ in your UI flow before forwarding to validation.

Example fix

// before
validateEmail(req.body.email) // req.body.email is null when omitted

// after
const email = req.body.email
if (typeof email !== 'string' || email.trim() === '') {
  // reject: no email supplied
} else {
  validateEmail(email)
}
Defensive patterns

Strategy: validation

Validate before calling

function validateEmailSafe(v) {
  if (typeof v !== 'string' || v.trim() === '') {
    throw new TypeError('Email address missing')
  }
  return validateEmail(v.trim())
}

Type guard

const isNonEmptyStringEmail = (v) => typeof v === 'string' && v.trim().length > 0

Try / catch

try { validateEmail(email) } catch (e) { if (/Null or Empty/.test(e.message)) { /* no email provided */ } else throw e }

Prevention

When it happens

Trigger: Calling validateEmail('') (empty string from an unfilled form), validateEmail(null) (common DB/JSON null). Calling validateEmail(undefined) does NOT throw — it coerces to 'undefined' and the regex may match; same for numbers like validateEmail(123).

Common situations: Form submissions where the email field was empty and got coerced to ''; JSON payloads with explicit null for an absent email; a default of null instead of undefined; database rows returning null for missing emails.

Related errors


AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13). Data as JSON: /api/errors/77e210448c5807a9. Report an issue: GitHub.