Replace the Last Character in a String in JavaScript

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

Replace the last character in a String #

To replace the last character in a string:

  1. Use the String.slice() method to get a portion of the string up until the last character.
  2. Use the addition (+) operator to add the replacement.
  3. The new still will contain the replacement character at the end.
index.js
const str = 'Hello World.'; const replaced = str.slice(0, -1) + '!'; console.log(replaced); // 👉️ Hello World!
JavaScript indexes are zero-based. The first character in a string has an index of 0 and the last an index of str.length - 1.

The parameters we passed to the String.slice method are:

  1. start index - the index of the first character to be included in the returned string.
  2. end index - go up to, but not including this index. A negative index indicates an offset from the end of the string.
Passing a negative index parameter of -1 and str.length - 1 is the same. We instruct the slice method to go up to, but not including the last character in the string.

By getting a part of the string up to, but not including the last character, we are able to replace it with one or more characters.

This approach also works if we need to replace the last character with multiple characters.

index.js
const str = 'Hello World.'; const replaced = str.slice(0, -1) + '!!!'; console.log(replaced); // 👉️ Hello World!!!
The slice method doesn't change the contents of the original string, it returns a new string containing a part of the original string.

An alternative, but also very common approach is to use the String.replace method.

Replace the last character in a String using replace() #

Use the String.replace() method to replace the last character in a string, e.g. const replaced = str.replace(/.$/, 'replacement');. The replace method will return a new string with the last character replaced by the provided replacement.

index.js
const str = 'Hello World.'; const replaced = str.replace(/.$/, '!'); console.log(replaced); // 👉️ Hello World!

The parameters we passed to the replace method are:

  1. a regular expression to match in the string
  2. a string replacement for the first match of the regular expression
The forward slashes / / mark the beginning and end of the regular expression.

The . character in our regular expression matches any single character.

The $ character matches the end of a string.

This satisfies our requirement to match and replace the last character in the string.

If you ever need help reading a regular expression, check out this regular expression cheatsheet by MDN.

The replace method doesn't mutate the original string, it returns a new string. Strings are immutable in JavaScript.

I prefer using the slice() method over using replace() because many developers are not familiar with regular expressions.

Further Reading #

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.