Get the last Character of a String in JavaScript

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Check out my new book

Get the last Character of a String #

To get the last character of a string, call the charAt() method on the string, passing it the last index as a parameter. For example, str.charAt(str.length - 1) returns a new string containing the last character of the string.

index.js
const str = 'abcde'; // ✅ get last character of a string using charAt() const last = str.charAt(str.length - 1); console.log(last); // 👉️ e // ✅ Get last character of a string using slice() const lst = str.slice(-1); console.log(lst); // 👉 'e' const lst2 = str.slice(-2); console.log(lst2); // 👉️ 'de'

The parameter we passed to the String.charAt method is the index.

Indexes are zero-based in JavaScript. The first character in the string has an index of 0, so the last character in the string has an index of str.length - 1.

If passed an index that doesn't exist, the charAt() method returns an empty string.

index.js
const str = ''; const last = str.charAt(str.length - 1); console.log(last); // 👉️ ""

An alternative approach is to directly access the string at the last index.

To get the last character of a string, use bracket notation to access the string at the last index, e.g. str[str.length - 1]. Indexes are zero-based, so the index of the last character in the string is str.length - 1.

index.js
const str = 'abcde'; const last = str[str.length - 1]; console.log(last); // 👉️ e

Accessing the character at the last index achieves the same result as the charAt method.

However, if we try to access a character at an index that doesn't exist, we get undefined back.

index.js
const str = ''; const last = str[str.length - 1]; console.log(last); // 👉️ undefined
This is the main reason I prefer the charAt method, which returns an empty string when supplied a non-existent index.

It's always easier to reason about and manage an application if we're consistent with types.

Get the last Character of a String using slice() #

Use the String.slice() method to get the last character of a string, e.g. const lst = str.slice(-1);. When passed an index of -1, the slice() method returns the last character of the string.

index.js
const str = 'abcde'; const last = str.slice(-1); console.log(last); // 👉 'e' const last2 = str.slice(-2); console.log(last2); // 👉️ 'de' const last3 = str.slice(-3); console.log(last3); // 👉️ 'cde'

The parameter we passed to the String.slice method is the start index.

The slice() method can be passed a negative start index to start slicing from the end of the string.

When passed an index of -1, the slice method returns the last character of the string.

Passing a negative index of -3 means give me the last 3 characters of the string.

This approach can be used to get the last N characters of a string.

The slice() method returns an empty string if the specified index is not found in the string.

index.js
const str = ''; const last = str.slice(-1); console.log(last); // 👉 ''

Further Reading #

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.