This post will discuss how to remove characters from the end of a string in JavaScript.

To remove characters from the end of a string in JavaScript, we need to use some function or function that can extract a part of the string from a start index to an end index. There are several ways to do achieve in JavaScript:

1. Using slice() function

The slice() function returns a part of the string from a start index to an end index. This function does not change the original string, but returns a new string. To remove characters from the end of a string, we can use a negative value for the end index, which means the end index is counted from the end of the string.

 
For example, we can use slice(0, -1) to remove the last character from a string. This will return a new string that contains all characters except the last one. Similarly, to remove last 6 characters from the string "Hello world", we can use slice(0, -6). This will return a new string that starts from index 0 (the first character) and ends at index -6 (the seventh last character), as demonstrated below:

Download  Run Code

2. Using substring() function

This is another way to remove characters from the end of a string by using the substring() function, which is similar to the slice() function, but has some differences. The substring() function also returns a part of the string from a start index to an end index, but it does not accept negative values for the indexes. Instead, it treats them as zero. To remove characters from the end of a string using the substring() function, we can use the length property of the string to calculate the end index.

 
For example, to remove n characters from the end of the string "Hello world", we can use substring(0, str.length - n). This will return a new string that starts from index 0 (the first character) and ends at index str.length - n.

Download  Run Code

3. Using replace() function

This is a more advanced and flexible way to remove characters from the end of a string by using the replace() function and a regular expression. The replace() function returns a new string with some or all matches of a pattern replaced by a replacement value. The pattern can be a string or a regular expression that defines what to match and replace. To remove characters from the end of a string, we can use a special syntax that matches any character (.) exactly n times ({n}) at the end of the string ($).

 
For example, we can use the regular expression /.{6}$/ to remove the last 6 characters from a string. The regular expression matches the six characters at the end of the string. The replacement value is an empty string (""), which will replace each matched substring with an empty string.

Download  Run Code

 
There is a lot more we can do we regular expressions. For example, to remove any number of exclamation marks (!) from the end of a string, we can use replace(/!+$/, ""). This will return a new string that contains all characters except the trailing exclamation marks. For instance:

Download  Run Code

That’s all about removing characters from the end of a string in JavaScript.