Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Masking email to hide it in JavaScript
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
Advertisements
