

Str.replace( /.$/, '') // Masteringjs.io Advanced Features Str.substr( 0, str.length - 1) // Masteringjs.io Str.substring( 0, str.length - 1) // Masteringjs.io replace(/.$/, '') replaces the last character of the string with an empty string. Using /.$/ as the regular expression argument matches the last character of the string, so. Replace() takes either a string or a regular expression as its pattern argument. Substring() does not have negative indexing, so be sure to use str.length - 1 when removing the last character from the string. Slice() is generally easier, however other methods available are substring() and replace().

Str.slice( 0, -1) // Masteringjs.io Alternative Methods

Slice() supports negative indexing, which means that slice(0, -1) is equivalent to slice(0, str.length - 1). It takes two arguments: the start index and the end index. To remove the last character from a string in JavaScript, you should use the slice() method.
