HOWTO · JavaScript
How to Remove Last Character From String in JavaScript
This tutorial teaches how to remove the last character from a JavaScript string.
On this page
JavaScript strings are immutable, so removing a character does not change the original string. It returns a new string with the selected part omitted. For the usual case, use slice(0, -1): the negative end index counts back from the end, so the result excludes the final UTF-16 code unit. If “character” means a user-perceived symbol, see the Unicode notes below because one symbol can contain more than one code unit.
Run this example:
const value = 'DelftStacks';
const shortened = value.slice(0, -1);
console.log(shortened);
DelftStack
The example produces DelftStack. It calls slice() with a start index of 0 and an end index one code unit before the end; the original value remains unchanged.
Use slice() to remove the last character
slice() accepts a start index and an optional end index. The end index is exclusive, so the character at that position is not included. A negative index is counted from the end of the string; therefore slice(0, -1) keeps everything before the last UTF-16 code unit. This is the concise default when that is the behavior you want.
Because strings are immutable, assign the returned value if you need to use it later: const shortened = value.slice(0, -1). The original variable still refers to its original string unless you assign the result back to a let variable.
Use substring() or substr() to remove the last character
substring() takes a start index and an exclusive end index. To omit the final code unit, use value.substring(0, value.length - 1). Its one-argument form starts at the supplied index and continues to the end; it does not remove the final character by itself.
The legacy substr() method instead takes a start index and a length. For example, value.substr(0, value.length - 1) asks for all but the final code unit. Prefer slice() or substring() in new code, since substr() is a legacy API and its length argument is easy to confuse with an end index.
Use replace() with a regular expression
replace() can remove a final character with value.replace(/.$/, ''). In this pattern, $ anchors the match at the end of the input, while . matches one character other than a line terminator by default. The replacement is an empty string, so the matched character is omitted. Because $ can match just before a final line terminator, a string ending in a newline may cause this pattern to remove the character before the newline instead of the newline itself. Use a string method when you need to remove the final code unit consistently.
Handle empty strings, line breaks, and Unicode characters
For an empty string, slice(0, -1) and substring(0, value.length - 1) return an empty string. They do not fail. For a one-code-unit string, the result is also empty. These methods count UTF-16 code units, which is how JavaScript string indexes and length are defined.
That distinction matters for emoji and other symbols represented by a surrogate pair: removing one code unit can leave an unpaired surrogate rather than remove the whole symbol. If the task is to remove one Unicode code point, use Array.from(value).slice(0, -1).join(''); this treats each code point as one item. A user-perceived grapheme can still contain multiple code points, such as a base letter plus a combining mark or a joined emoji sequence. For that case, use grapheme segmentation with Intl.Segmenter and remove the final segment.
A trailing line break is removed by slice(0, -1), since it removes the final code unit regardless of its value. The regular expression /.$/ behaves differently because dot does not match line terminators by default. Choose the method based on whether the goal is to remove the final code unit, a code point, or a grapheme; those are different operations.