How to delete the first word from a string using JavaScript?

By IncludeHelp Last updated : November 15, 2023

To delete the first word from a given string/line, use the substr() method to get the string without the first word. Here, you need to get the index of the first space (using indexOf() method) and then get the string from the next character. It will return a string without containing the first word.


The syntax to delete the first word from a string:

str.substr(str.indexOf(" ") + 1);
//Here, "str" is the string variable.


In this example, we have a string and we are deleting its first word using the JavaScript.

// Function to remove first word from a string
function removeFirstWord() {
    // string
    let str = 'Hello World';
    // Replace method to remove first word
    let result = str.substr(str.indexOf(" ")+1);
    // Display result
// Function call



Comments and Discussions!

Copyright © 2023 All rights reserved.