In this article, I am going to show you some ways to get not just the first character but any character at any position of a string.


Method #1: string.charAt(index)

The String.prototype.charAt() method returns a string representing the character at the specified index.

const string = 'Hello World';
string.charAt(0); // 'H' - the first character
string.charAt(1); // 'e' - the second character

If the input string is empty, this method returns an empty string.

const string = '';
string.charAt(0); // ''

Method #2: string.slice(startIndex, endIndex)

The String.prototype.slice() method returns the part of the string between the start (inclusive) and the end (exclusive) indexes or the end of the string if the end index is omitted.

const string = 'Hello World';
string.slice(0, 1); // 'H' - the first character
string.slice(1, 2); // 'e' - the second character

Method #3: string.substring(startIndex, endIndex)

Similar to the String.prototype.slice() method, you can use the String.prototype.substring() method instead.

const string = 'Hello World';
string.substring(0, 1); // 'H' - the first character
string.substring(1, 2); // 'e' - the second character

Method #4: string.substr(startIndex, length) [DEPRECATED]

The String.prototype.substr() method returns the part of the string starting at the start index with the specified length. This method is deprecated so you should avoid using it.

Method #5: string[index] {DANGEROUS}

Similar to the String.prototype.charAt() method, the bracket notation is used for character access.

const string = 'Hello World';
string[0]; // 'H' - the first character
string[1]; // 'e' - the second character

Note that this returns the undefined value if the input string is empty.

const string = '';
string[0]; // undefined

I recommend not using this method. Since the bracket notation is available for arrays too, you do not know the target object is a string or an array.


There are many ways to get the first character of a string, but in my opinion, the Method #1 that using the charAt() method is the best because of its clarity.

Here are 2 more notes:

  • You can use those methods to get any character at any position of a string
  • The slice() and substring methods can extract the substring with any length, not only a single character