{"record":{"id":"77e210448c5807a9","repo":"TheAlgorithms/JavaScript","slug":"email-address-string-null-or-empty","errorCode":null,"errorMessage":"Email Address String Null or Empty.","messagePattern":"Email Address String Null or Empty\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"warning","filePath":"String/ValidateEmail.js","lineNumber":6,"sourceCode":"/**\n * Returns whether the given string is a valid email address or not.\n */\nconst validateEmail = (str) => {\n  if (str === '' || str === null) {\n    throw new TypeError('Email Address String Null or Empty.')\n  }\n\n  return /^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/.test(str)\n}\n\nexport { validateEmail }\n","sourceCodeStart":1,"sourceCodeEnd":13,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/String/ValidateEmail.js#L1-L13","documentation":"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).","triggerScenarios":"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).","commonSituations":"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.","solutions":["Check for empty/null before calling, and also enforce typeof for safety: if (typeof str !== 'string' || !str.trim()) reject.","Normalize null/undefined upstream so an absent email never reaches validation.","Use a stricter validator that type-checks first, since this guard misses undefined and non-strings.","Treat null as 'no email provided’ in your UI flow before forwarding to validation."],"exampleFix":"// before\nvalidateEmail(req.body.email) // req.body.email is null when omitted\n\n// after\nconst email = req.body.email\nif (typeof email !== 'string' || email.trim() === '') {\n  // reject: no email supplied\n} else {\n  validateEmail(email)\n}","handlingStrategy":"validation","validationCode":"function validateEmailSafe(v) {\n  if (typeof v !== 'string' || v.trim() === '') {\n    throw new TypeError('Email address missing')\n  }\n  return validateEmail(v.trim())\n}","typeGuard":"const isNonEmptyStringEmail = (v) => typeof v === 'string' && v.trim().length > 0","tryCatchPattern":"try { validateEmail(email) } catch (e) { if (/Null or Empty/.test(e.message)) { /* no email provided */ } else throw e }","preventionTips":["Type-check email fields at the boundary — this guard misses undefined and non-strings.","Normalize null to undefined and treat both as 'no email' before validating.","Prefer a stricter email validator for production; this regex is intentionally simple."],"tags":["string","validation","email","type-check","input-validation"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}