How to Remove Everything After a Specific Character in JavaScript?

Estimated read time 1 min read

To remove everything after a specific character in JavaScript, you can use the substring() method. Here’s an example:

let str = "This is a string";
let index = str.indexOf(" ");
let newStr = str.substring(0, index);

This code removes everything after the first space character from the string "This is a string" using the substring() method and logs the modified string: "This".

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply