Get the Last Word of a String in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 1, 2024
3 min

banner

# Get the Last Word of a String in JavaScript

To get the last word of a string in JavaScript:

  1. Use the String.split() method to split the string into an array of words.
  2. Use the Array.pop() method to get the last word from the array.
index.js
const str = 'An example sentence'; const lastWord = str.split(' ').pop(); console.log(lastWord); // ๐Ÿ‘‰๏ธ sentence

get last word of string

The code for this article is available on GitHub

We use the String.split() method to get an array containing the words in the string.

index.js
const str = 'An example sentence'; const split = str.split(' ') console.log(split) // ๏ธ๐Ÿ‘‰๏ธ ['An', 'example', 'sentence']
The argument we passed to the String.split() method is the separator on which we want to split the string.

We used a space as the separator to split the string into an array of words.

The Array.pop() method removes and returns the last element (word) from the array.

If your string ends with a space, use the String.trim() method to remove any leading and trailing whitespace before calling the String.split() method.

index.js
const str = 'An example sentence '; const lastWord = str.trim().split(' ').pop(); console.log(lastWord); // ๐Ÿ‘‰๏ธ sentence
We used the String.trim() method to remove the leading and trailing whitespace from the string and then called the String.split() method.

This is necessary because if the string ends with a whitespace character, the last element in the array would be an empty string.

index.js
const str = 'An example sentence '; // ๐Ÿ‘‡๏ธ [ 'An', 'example', 'sentence', '' ] console.log(str.split(' '));

# Get the Last Word of a String using string indexing

As an alternative to using the Array.pop() method, you can also access the array at its last index.

  1. Use the String.split() method to split the string into an array of words.
  2. Access the array at index [arr.length - 1] to get the last word.
index.js
const str = 'An example sentence'; const arr = str.split(' '); console.log(arr); // ๐Ÿ‘‰๏ธ ['An', 'example', 'sentence'] const lastWord = arr[arr.length - 1]; console.log(lastWord); // ๐Ÿ‘‰๏ธ sentence

get last word of string using string indexing

The code for this article is available on GitHub

The code sample is very similar to the last one, but instead of using the Array.pop() method, we used bracket notation to get the last element of the array.

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

You can also use the Array.at() method to get the last element of an array.

index.js
const str = 'An example sentence'; const arr = str.split(' '); console.log(arr); // ๐Ÿ‘‰๏ธ ['An', 'example', 'sentence'] const lastWord = arr.at(-1); console.log(lastWord); // ๐Ÿ‘‰๏ธ sentence

The Array.at() method takes an integer value and returns the element at that index.

The method supports positive and negative indexing.

Negative indices can be used to count backward, e.g. an index of -1 returns the last element in the array, -2 returns the second to last element, etc.

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

# Get the Last Word of a String using String.slice()

This is a two-step process:

  1. Use the String.lastIndexOf() method to get the index of the last space in the string.
  2. Use the String.slice() method to get the part of the string after the last space.
index.js
const str = 'An example sentence'; const lastWord = str.slice(str.lastIndexOf(' ') + 1); console.log(lastWord); // ๐Ÿ‘‰๏ธ sentence

get last word of string using string slice

The code for this article is available on GitHub

We used the String.lastIndex() method to get the index of the last space in the string.

index.js
const str = 'An example sentence'; console.log(str.lastIndexOf(' ')); // ๐Ÿ‘‰๏ธ 10 console.log(str.lastIndexOf(' ') + 1); // ๐Ÿ‘‰๏ธ 11

We had to add 1 to the result because we didn't want to include the space in the output.

The last step is to use the String.slice() method to get the part of the string after the last space.

The only argument we passed to the String.slice() method is the start index - the index of the first character to be included in the new string.

# 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