Sometimes, we want to a remove portion of string in JavaScript.
In this article, we’ll look at how to a remove portion of string in JavaScript.
How to a remove portion of string in JavaScript?
To a remove portion of string in JavaScript, we can call the string replace
method with the substring we want to remove and replace it with an empty string.
For instance, we write:
const string = '@usernameWhats on your mind?'
const newString = string.replace('Whats on your mind?', '');
console.log(newString)
We call string.replace
to replace 'Whats on your mind?'
with an empty string and return the new string.
Therefore, newString
is '@username'
.
Conclusion
To a remove portion of string in JavaScript, we can call the string replace
method with the substring we want to remove and replace it with an empty string.