×

Index

JavaScript Tutorial

JavaScript Examples

CSS Tutorial

CSS Examples

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

By IncludeHelp Last updated : November 15, 2023

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

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.

Syntax

The syntax to delete the first word from a string:

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

Example

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
    console.log(result);
}
 
// Function call
removeFirstWord();

Output

World

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.