Mask an email address for logs
Keep the first character and the domain; hide the rest of the local part.
function maskEmail(address) { return `${address.slice(0, 1)}•••${address.slice(address.lastIndexOf('@'))}`; }
Input & expected output
Input
"[email protected]"Expected output
"a•••@example.com"The edge case
Input
"[email protected]"Expected output
"a•••@example.com"Know the limits
- Masking reduces exposure in logs; it is not anonymization, and the domain stays visible.
- The mask has a fixed width, so it does not reveal the length of the local part. Prefer logging a message or delivery identifier instead.
The function above is the implementation used by the lab. The lab validates JSON types and caps size before execution; include equivalent validation when accepting untrusted input in your application.
Source & maintenance
Written by Oveyon for this edition of 1loc.dev; it is not part of the original collection. Copyright (c) 2026 Oveyon. MIT licensed.
The function above is the implementation the lab runs and the tests exercise. Read the license.
Keep exploring
- Convert an email domain to punycode — Keep the local part and convert an international domain to its ASCII xn-- form.
- Get the domain of an email address — Take the part after the last @ and lowercase it, for domain checks and grouping.
- Normalize an email address for comparison — Trim whitespace and lowercase only the domain, preserving the local part.