How to Remove the Last Word from the String Using JavaScript

Tutorials rack
1 min readJun 15, 2021
Remove Last Word in javascript — Tutorialsrack

In this article, you will learn how to remove the last word from the string using Javascript. There are many ways to remove the last word in the string using javascript.

Here are the various examples for removing the last word in the string using javascript functions.

Example 1: Using string.substring() Function

var str = “I want to remove the last word.”;

// Get the Last index of Space

// or any specified character in place of space

var index = str.lastIndexOf(“ “);

// returns the string from 0 to specified index

str = str.substring(0, index);

console.log(str)

//Output ==> “I want to remove the last”

Example 2: Using split(), pop() and join() Function

var str = “I want to remove the last word.”;

//split by space

//or any specified character in place of space

var res = str.split(“ “);

//remove last element

res.pop();

//join back together

console.log(res.join(“ “));

//Output ==> “I want to remove the last”

I hope this article will help you to understand how to remove the last word from the string in Javascript.

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!

--

--

Tutorials rack

TutorialsRack.com is optimized for learning web technologies step by step. Examples might be simplified to improve reading and basic understanding.