Masking email to hide it in JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

It is a common practice that when websites display anyone's private email address they often mask it in order to maintain privacy.

Therefore for example −

If someone's email address is −

const email = 'ramkumar@example.com';

Then it is displayed like this −

const masked = 'r...r@example.com';

We are required to write a JavaScript function that takes in an email string and returns the masked email for that string.

Example

Following is the code −

const email = 'ramkumar@example.com';
const maskEmail = (email = '') => {
   const [name, domain] = email.split('@');
   const { length: len } = name;
   const maskedName = name[0] + '...' + name[len - 1];
   const maskedEmail = maskedName + '@' + domain;
   return maskedEmail;
};
console.log(maskEmail(email));

Output

Following is the output on console −

r...r@example.com
raja
Updated on 11-Dec-2020 09:21:51

Advertisements