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 = '[email protected]';
Then it is displayed like this −
const masked = '[email protected]';
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 = '[email protected]';
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 −
[email protected]