Last updated: Mar 1, 2024
Reading timeยท3 min
To get the last word of a string in JavaScript:
String.split()
method to split the string into an array of words.Array.pop()
method to get the last word from the array.const str = 'An example sentence'; const lastWord = str.split(' ').pop(); console.log(lastWord); // ๐๏ธ sentence
We use the String.split() method to get an array containing the words in the string.
const str = 'An example sentence'; const split = str.split(' ') console.log(split) // ๏ธ๐๏ธ ['An', 'example', 'sentence']
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.
const str = 'An example sentence '; const lastWord = str.trim().split(' ').pop(); console.log(lastWord); // ๐๏ธ sentence
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.
const str = 'An example sentence '; // ๐๏ธ [ 'An', 'example', 'sentence', '' ] console.log(str.split(' '));
As an alternative to using the Array.pop()
method, you can also access the
array at its last index.
String.split()
method to split the string into an array of words.[arr.length - 1]
to get the last word.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
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.
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.
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.
String.slice()
This is a two-step process:
String.lastIndexOf()
method to get the index of the last space in
the string.String.slice()
method to get the part of the string after the last
space.const str = 'An example sentence'; const lastWord = str.slice(str.lastIndexOf(' ') + 1); console.log(lastWord); // ๐๏ธ sentence
We used the String.lastIndex()
method to get the index of the last space in
the string.
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.
You can learn more about the related topics by checking out the following tutorials: