Get last char or last N characters of String in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 1, 2024
6 min

banner

# Table of Contents

  1. Get the last Character of a String in JavaScript
  2. Get the last N characters of a String in JavaScript

# Get the last Character of a String in JavaScript

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 the last character of a string using charAt() const last = str.charAt(str.length - 1); console.log(last); // ๐Ÿ‘‰๏ธ e // โœ… Get the 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' // โœ… Get the last character of a string using String.at() const last_ = str.at(-1); console.log(last_); // ๐Ÿ‘‰๏ธ e

get last character of string

The code for this article is available on GitHub

The argument we passed to the String.charAt() method is the index.

JavaScript indexes are zero-based, so the first character in the string has an index of 0 and 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); // ๐Ÿ‘‰๏ธ ""

# Get the last Character of a String using indexing

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

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

get last character of string using indexing

The code for this article is available on GitHub

Accessing the character at the last index achieves the same result as using 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 with a non-existent index.

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

Note: If you need to get the last N characters of a string, click on the following subheading:

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

Alternatively, you can use the String.slice() method.

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'

get last character of string using slice

The code for this article is available on GitHub

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

The slice() method can be passed a negative start index to count backward.

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'

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); // ๐Ÿ‘‰ ''

Note: If you need to get the last N characters of a string, click on the following subheading:

# Get the last character of a String using String.at()

You can also use the String.at() method.

The String.at() method can be passed a value of -1 to return the last character of a string as it supports negative indexing.

index.js
const str = 'bobbyhadz'; const last = str.at(-1); console.log(last); // ๐Ÿ‘‰๏ธ z

get last character of string using string at

The code for this article is available on GitHub

We used the String.at() method to get the last characters of a string.

The method takes an integer that represents the index and returns the character at the specified index.

The method supports negative integers to count backward. For example, -1 returns the last character in the string and -2 returns the second last character.

index.js
const str = 'bobbyhadz'; const last = str.at(-1); console.log(last); // ๐Ÿ‘‰๏ธ z console.log(str.at(-2)); // ๐Ÿ‘‰๏ธ d console.log(str.at(-3)); // ๐Ÿ‘‰๏ธ a

We passed a value of -1 to the String.at() method to get the last character in the string.

If you pass an index that is out of range to the String.at() method, the method returns undefined.

index.js
const str = ''; console.log(str.at(-1)); // ๐Ÿ‘‰๏ธ undefined

Which approach you pick is a matter of personal preference. I'd use the String.slice() method because it returns an empty string when the supplied index is out of range.

# Get the last N characters of a String in JavaScript

Pass -N as an argument to the slice() method to get the last N characters of a string.

For example, str.slice(-2) returns the last 2 characters of the string.

index.js
const str = 'bobbyhadz.com'; // โœ… get the last 2 characters of a string const last2 = str.slice(-2); console.log(last2); // ๐Ÿ‘‰๏ธ om // โœ… get the last 3 characters of a string const last3 = str.slice(-3); console.log(last3); // ๐Ÿ‘‰๏ธ com // โœ… get the last 4 characters of a string const last4 = str.slice(-4); console.log(last4); // ๐Ÿ‘‰ .com

get last n characters of string

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.

Make sure to assign the result of calling the slice() method to a variable as the method doesn't change the original string.

The String.slice() method can be passed negative indexes to count backward.

index.js
const str = 'bobbyhadz.com'; console.log(str.slice(-2)); // ๐Ÿ‘‰๏ธ om console.log(str.slice(-5)); // ๐Ÿ‘‰๏ธ z.com
Passing a negative start index to the slice method is the same as specifying a start index of string.length - N.
index.js
const str = 'bobbyhadz.com'; const last3 = str.slice(-3); // ๐Ÿ‘‰๏ธ com console.log(last3); const last3Again = str.slice(str.length - 3); console.log(last3Again); // ๐Ÿ‘‰๏ธ com

In both examples, we tell the slice method to copy the last 3 characters of the string into a new string.

If we try to get more characters than the string contains, slice() returns the entire string and doesn't throw an error.
index.js
const str = 'bobbyhadz.com'; const last100 = str.slice(-100); console.log(last100); // ๐Ÿ‘‰๏ธ bobbyhadz.com

In this example, we tried to get the last 100 characters of a string that only contains 13 characters.

As a result, the slice method returned a copy of the entire string.

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

index.js
function getLastNchars(str, n) { return str.slice(-n); } const str = 'bobbyhadz.com'; // โœ… get the last 2 characters of a string console.log(getLastNchars(str, 2)); // ๐Ÿ‘‰๏ธ om // โœ… get the last 3 characters of a string console.log(getLastNchars(str, 3)); // ๐Ÿ‘‰๏ธ com // โœ… get the last 4 characters of a string console.log(getLastNchars(str, 4)); // ๐Ÿ‘‰๏ธ .com

define reusable function

The getLastNchars function takes a string and N as parameters and returns the last N characters of the string.

# Get the last N characters of a string using String.substring

Alternatively, you can use the String.substring() method.

For example, str.substring(str.length - 3) returns the last 3 characters of the string.

index.js
function getLastNchars(str, n) { return str.substring(str.length - n); } const str = 'bobbyhadz.com'; // โœ… get the last 2 characters of a string console.log(getLastNchars(str, 2)); // ๐Ÿ‘‰๏ธ om // โœ… get the last 3 characters of a string console.log(getLastNchars(str, 3)); // ๐Ÿ‘‰๏ธ com // โœ… get the last 4 characters of a string console.log(getLastNchars(str, 4)); // ๐Ÿ‘‰๏ธ .com

get last n characters 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.

The String.substring() method doesn't support negative values to count backward, so we had to calculate the start index based on the string's length.

index.js
const str = 'bobbyhadz.com'; // ๐Ÿ‘‡๏ธ om console.log(str.substring(str.length - 2));
The slice and substring methods are similar, however, you should use the slice method because its implementation is more intuitive.

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.

For these reasons, it is recommended to use the String.slice() method to get the last N characters of a string.

The slice method works in a more predictable manner.

The code for this article is available on GitHub

# 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