Get the first Character of a String in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
4 min

banner

# Get the first Character of a String in JavaScript

To get the first character of a string, call the String.charAt() method with an index of 0.

The method will return a new string that only contains the first character of the original string.

index.js
const str = 'bobbyhadz.com'; const firstChar = str.charAt(0); console.log(firstChar); // ๐Ÿ‘‰๏ธ b

get first character of string

The code for this article is available on GitHub

The String.charAt() method returns the character at the specified index.

If the index doesn't exist in the string, the method returns an empty string.

index.js
console.log('abc'.charAt(0)); // ๐Ÿ‘‰๏ธ a console.log(''.charAt(0)); // ๐Ÿ‘‰๏ธ ""
JavaScript indexes are zero-based. The first character in the string has an index of 0 and the last character has an index of str.length - 1.

If you call the String.charAt() method with an index that doesn't exist, it returns an empty string.

index.js
const str = ''; const firstChar = str.charAt(0); console.log(firstChar); // ๐Ÿ‘‰๏ธ ""

# Get the first Character of a String using bracket notation

An alternative approach is to directly access the index.

Accessing the string at index 0 returns the first character of the string.

index.js
const str = 'bobbyhadz.com'; const firstChar = str[0]; console.log(firstChar); // ๐Ÿ‘‰๏ธ b console.log('abcd'[0]); // ๐Ÿ‘‰๏ธ a

get first character of string using bracket notation

The code for this article is available on GitHub

If we access the string at the specific index, we can avoid calling the charAt() method.

However, if you try to access the string at an index that doesn't exist, undefined is returned.

index.js
const str = ''; const first = str[0]; console.log(first); // ๐Ÿ‘‰๏ธ undefined

This is the reason I prefer using the String.charAt() method.

I'd rather get an empty string than an undefined value if I access a string at an index that doesn't exist.

# Get the first Character of a String using String.at()

The String.at() method returns a new string containing the character at the specified index.

index.js
const str = 'bobbyhadz.com'; const firstChar = str.at(0); console.log(firstChar); // ๐Ÿ‘‰๏ธ b console.log('abcd'.at(0)); // ๐Ÿ‘‰๏ธ a console.log(''.at(0)); // ๐Ÿ‘‰๏ธ undefined

get first character of string using string at

The code for this article is available on GitHub

The String.at() method takes an integer and returns a new string containing the character at the specified position.

The String.at method allows for positive and negative integers.

You can use negative integers to count back from the end of the string.

index.js
const str = 'bobbyhadz.com'; const firstChar = str.at(0); console.log(firstChar); // ๐Ÿ‘‰๏ธ b console.log(str.at(-1)); // ๐Ÿ‘‰๏ธ m console.log(str.at(-2)); // ๐Ÿ‘‰๏ธ o console.log(str.at(-3)); // ๐Ÿ‘‰๏ธ c

The String.at() method is quite convenient when you need to count backward.

The String.charAt() method and bracket notation approach doesn't support negative indexing.

index.js
console.log(str.at(-1)); // ๐Ÿ‘‰๏ธ m console.log(str.charAt(-1)); // ๐Ÿ‘‰๏ธ "" console.log(str[-1]); // ๐Ÿ‘‰๏ธ undefined

You can also use the String.slice() method to get the first character of a string.

# Get the first Character of a String using String.slice()

Specify a start index of 0 and a stop index of 1 to get the first character of a string using the slice() method.

index.js
const str = 'bobbyhadz.com'; const firstChar = str.slice(0, 1); console.log(firstChar); // ๐Ÿ‘‰๏ธ b

get first character of string 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

When only a single argument is passed to the String.slice() method, the slice goes to the end of the string.

We used a start index of 0 and a stop index of 1 to get the first character in a string.

The stop index is exclusive (up to, but not including), so the second character is not contained in the result.

If you try to get the first character of an empty string using slice(), an empty string is returned.

index.js
console.dir(''.slice(0, 1)); // ๐Ÿ‘‰๏ธ ''

# Get the first Character of a String using String.substring()

You can also use the String.substring() method in a similar way.

index.js
const str = 'bobbyhadz.com'; const firstChar = str.substring(0, 1); console.log(firstChar); // ๐Ÿ‘‰๏ธ b

get first character of string using substring

The code for this article is available on GitHub

The String.substring() method returns a slice of the string from the start index to the excluding end index.

The method takes the following parameters:

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

If no end index is specified the slice goes to the end of the string.

If you try to get the first character of an empty string using substring(), an empty string is returned.

index.js
console.dir(''.substring(0, 1)); // ๐Ÿ‘‰๏ธ ''

The substring() and slice() method seem identical from the previous 2 code samples.

However, there are a couple of differences between the String.substring() and the String.slice() methods:

  • The substring() method swaps its start and end index if the start index is greater than the end index. The slice() method returns an empty string in this case.
index.js
const str = 'bobby'; console.log(str.substring(3, 0)); // ๐Ÿ‘‰๏ธ bob console.log(str.slice(3, 0)); // ๐Ÿ‘‰๏ธ ''
  • If either of both arguments passed to substring() are negative, they are treated as if they were 0.
index.js
const str = 'bobby'; console.log(str.substring(-3)); // ๐Ÿ‘‰๏ธ bobby console.log(str.slice(-3)); // ๐Ÿ‘‰๏ธ bby

When given a negative index, the slice() method counts backward from the end of the string to find the indexes.

# Get the first Character of a String using Array.from()

When working with Unicode strings that might contain emojis or other special characters, you might have to convert the string to an array of characters using Array.from().

index.js
const str = '๐Ÿดbobbyhadz.com'; const firstChar = Array.from(str)[0]; console.log(firstChar); // '๐Ÿด'

get first character of string using array from

The code for this article is available on GitHub

The Array.from() method converts the supplied iterable to an array.

index.js
const str = '๐Ÿดbobbyhadz.com'; // [ // '๐Ÿด', 'b', 'o', 'b', // 'b', 'y', 'h', 'a', // 'd', 'z', '.', 'c', // 'o', 'm' // ] console.log(Array.from(str));

The last step is to access the character at index 0.

# 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