Remove the Last Word from a String using JavaScript

avatar

Borislav Hadzhiev

Fri Oct 22 20213 min read

Remove the Last Word from a String using JavaScript #

To remove the last word from a string, get the index of the last space in the string, using the lastIndexOf() method. Then use the substring() method to get a portion of the string with the last word removed.

index.js
function removeLastWord(str) { const lastIndexOfSpace = str.lastIndexOf(' '); if (lastIndexOfSpace === -1) { return str; } return str.substring(0, lastIndexOfSpace); } console.log(removeLastWord('Hello World')); // ๐Ÿ‘‰๏ธ Hello console.log(removeLastWord('hello')); // ๐Ÿ‘‰๏ธ hello console.log(removeLastWord('one two three')); // ๐Ÿ‘‰๏ธ one two

We created a reusable function to remove the last word from a string.

The first thing we do is use the String.lastIndexOf method to get the index of the last space in the string.

The lastIndexOf method returns the index of the last occurrence of a substring in a string. If the substring is not contained in the string the method returns -1.

To cover the scenario that the string only contains a single word (no spaces), we check if the lastIndexOf method returns -1, and if it does we return the string, without removing any words.

If you want to remove the word, even if the string contains a single word, you can remove the if statement.
index.js
function removeLastWord(str) { const lastIndexOfSpace = str.lastIndexOf(' '); // ๐Ÿ‘‡๏ธ remove this if you want to delete single words too // if (lastIndexOfSpace === -1) { // return str; // } return str.substring(0, lastIndexOfSpace); }

The final step is to use the String.substring method to get a portion of the string without the last word.

We pass the following parameters to the substring method:

  1. start index - the index of the first character to be included in the new string
  2. end index - go up to, but not including this index
We basically get a new string without the last space and word of the original string.

An alternative approach is to use the slice and join methods.

To remove the last word from a string:

  1. Call the split() method on the string to get an array containing the words in the string.
  2. Use the slice() method to get a portion of the array with the last word removed.
  3. Call the join() method to join the array into a string.
index.js
function removeLastWord(str) { const words = str.split(' '); if (words.length === 1) { return str; } return words.slice(0, -1).join(' '); } console.log(removeLastWord('Hello World')); // ๐Ÿ‘‰๏ธ Hello console.log(removeLastWord('hello')); // ๐Ÿ‘‰๏ธ hello console.log(removeLastWord('one two three')); // ๐Ÿ‘‰๏ธ one two

We split the string on each space to get an array containing the words in the string.

index.js
// ๐Ÿ‘‡๏ธ ['hello', 'world'] console.log('hello world'.split(' '))

We also check if the string contains 1 or 0 words and return it if it does.

Then we use the Array.slice method to get a portion of the array where the last word in omitted.

We passed the following parameters to the slice method:

  • start index - index (zero-based) at which to start extraction.
  • end index - extract values up to, but not including this index. A negative index indicates an offset from the end of the array.
A negative index of -1 means go up to, but not including the last element (word) of the array.

The final step is to use the Array.join method to join the array into a string.

The parameter the join method takes is a separator, just like the split method, so we again pass a space as the separator.

Further Reading #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee