How to Remove the Last Word from a String in JavaScript?

Estimated read time 1 min read

To remove the last word from a string in JavaScript, you can use the slice() and lastIndexOf() methods. Here’s an example:

let str = "This is a string";
let newStr = str.slice(0, str.lastIndexOf(" "));

This code removes the last word from the string "This is a string" using the slice() and lastIndexOf() methods and logs the modified string: "This is a".

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply