Extract the Name from an Email Address in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 4, 2024
3 min

banner

# Extract the Name from an Email Address in JavaScript

To extract the name from an email address:

  1. Use the split() method to split the email on the @ symbol.
  2. Access the first array element.
  3. The first element in the array stores the name.
index.js
const email = 'bobbyhadz@email.com'; const result = email.split('@')[0]; console.log(result); // ๐Ÿ‘‰๏ธ "bobbyhadz"

extract name from email address

The code for this article is available on GitHub

The String.split() method takes a separator and splits the string into an array on each occurrence of the provided delimiter.

The String.split() method takes the following 2 parameters:

NameDescription
separatorThe pattern describing where each split should occur.
limitAn integer used to specify a limit on the number of substrings to be included in the array.

We split the string on the @ symbol.

index.js
const email = 'bobbyhadz@email.com'; // ๐Ÿ‘‡๏ธ [ 'bobbyhadz', 'email.com' ] console.log(email.split('@')); // ๐Ÿ‘‡๏ธ bobbyhadz console.log(email.split('@')[0]);

The first string in the array contains the username which we can access using an index of 0.

JavaScript indexes are zero-based, so the index of the first element in an array is 0, and the index of the last element is array.length - 1.

If you have to do this often, define a reusable function.

index.js
function extractNameFromEmail(email) { return email.split('@')[0]; } const email = 'bobbyhadz@email.com'; const name = extractNameFromEmail(email); console.log(name); // ๐Ÿ‘‰๏ธ bobbyhadz

The function takes an email as a parameter and extracts the name from the email.

# Handle the case where the string doesn't contain an @ symbol

If an @ symbol is not contained in the string, you'd get an array containing the entire string as a parameter.

index.js
const email = 'bobbyhadzemail.com'; // ๐Ÿ‘‡๏ธ [ 'bobbyhadzemail.com' ] console.log(email.split('@'));

handle case where string does not contain at symbol

The code for this article is available on GitHub

If you need to handle this scenario, use an if statement.

index.js
const email = 'bobbyhadzemail.com'; let name = ''; if (email.includes('@')) { name = email.split('@')[0]; } console.log(name); // ๐Ÿ‘‰๏ธ ""

The String.includes() method returns true if the supplied substring is contained in the string and false otherwise.

We only reassign the name variable, if the @ symbol is contained in the string.

An alternative approach is to use the String.slice() method.

# Extract the Name from an Email Address using String.slice

This is a two-step process:

  1. Use the String.indexOf() method to get the index of the @ character.
  2. Use the slice() method to get the part of the string up to the index.
index.js
const email = 'bobbyhadz@email.com'; const result = email.slice(0, email.indexOf('@')); console.log(result); // ๐Ÿ‘‰๏ธ "bobbyhadz"

extract name from email address using slice

The code for this article is available on GitHub

The String.slice() method extracts a section of a string and returns it, without modifying the original string.

The String.slice() method takes the following arguments:

NameDescription
start indexThe index of the first character to include in the returned substring
end indexThe index of the first character to exclude from the returned substring

The slice starts at index 0 and goes up to, but not including the index of the @ character.

We used the String.indexOf() method to get the index of the @ character in the string.

index.js
const email = 'bobbyhadz@email.com'; console.log(email.indexOf('@')); // ๐Ÿ‘‰๏ธ 9

The String.indexOf() method returns the index of the first occurrence of a substring in a string.

If the substring is not contained in the string, the method returns -1.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev