Replace the Last Character in a String in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 1, 2024
4 min

banner

# Replace the last character in a String in JavaScript

To replace the last character in a string:

  1. Use the String.slice() method to get a portion of the string up to the last character.
  2. Use the addition (+) operator to add the replacement.
  3. The new string will contain the replacement character at the end.
index.js
const str = 'bobbyhadz.com'; const replaced = str.slice(0, -1) + '_'; console.log(replaced); // ๐Ÿ‘‰๏ธ bobbyhadz.co_

replace last character in string

The code for this article is available on GitHub
JavaScript indexes are zero-based. The first character in a string has an index of 0 and the last character has an index of str.length - 1.

The String.slice() method extracts a section of a string and returns it, without modifying the original string.

The String.slice() method takes the following arguments:

NameDescription
start indexThe index of the first character to include in the returned substring
end indexThe index of the first character to exclude from the returned substring

The String.slice() method can be passed negative indexes to count backward.

index.js
const str = 'bobbyhadz.com'; console.log(str.slice(-3)); // ๐Ÿ‘‰๏ธ com console.log(str.slice(-3, -1)); // ๐Ÿ‘‰๏ธ co console.log(str.slice(0, -1)); // ๐Ÿ‘‰๏ธ bobbyhadz.co
Passing a negative index parameter of -1 and str.length - 1 is the same. We instruct the String.slice() method to go up to, but not including the last character in the string.

We practically get a slice of the string up to, but not including the last character and use the addition (+) operator to add the replacement character.

This approach also works if you need to replace the last character in a string with multiple characters.

index.js
const str = 'bobbyhadz.com'; const replaced = str.slice(0, -1) + '!!!'; console.log(replaced); // ๐Ÿ‘‰๏ธ bobbyhadz.co!!!
The slice() method doesn't change the contents of the original string. The method returns a new string containing a part of the original string.

If you have to replace the last character in a string often, create a reusable function.

index.js
function replaceLastCharacter(string, replacement) { return string.slice(0, -1) + replacement; } const str = 'bobbyhadz.com'; // ๐Ÿ‘‡๏ธ bobbyhadz.co_ console.log(replaceLastCharacter(str, '_')); // ๐Ÿ‘‡๏ธ bobbyhadz.co@ console.log(replaceLastCharacter(str, '@')); // ๐Ÿ‘‡๏ธ bobbyhadz.co!@#$ console.log(replaceLastCharacter(str, '!@#$'));

defining reusable function

The function takes the string and the replacement character as parameters and replaces the last character in the string.

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

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

index.js
const str = 'Hello World.'; const replaced = str.replace(/.$/, '!'); console.log(replaced); // ๐Ÿ‘‰๏ธ Hello World!

replace last character in string using string replace

The code for this article is available on GitHub

The String.replace() method returns a new string with one, some, or all matches of a regular expression replaced with the provided replacement.

The method takes the following parameters:

NameDescription
patternThe pattern to look for in the string. Can be a string or a regular expression.
replacementA string used to replace the substring match by the supplied pattern.

The String.replace() method returns a new string with the matches of the pattern replaced. The method doesn't change the original string.

Strings are immutable in JavaScript.

The forward slashes / / mark the beginning and end of the regular expression.

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

The dollar sign $ character matches the end of a string.

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

If you have to replace the last character in a string often, define a reusable function.

index.js
function replaceLastCharacter(string, replacement) { return string.replace(/.$/, replacement); } const str = 'bobbyhadz.com'; // ๐Ÿ‘‡๏ธ bobbyhadz.co_ console.log(replaceLastCharacter(str, '_')); // ๐Ÿ‘‡๏ธ bobbyhadz.co@ console.log(replaceLastCharacter(str, '@')); // ๐Ÿ‘‡๏ธ bobbyhadz.co!@#$ console.log(replaceLastCharacter(str, '!@#$'));

The function takes the string and the replacement character as arguments and replaces the last character in the string.

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

It contains a table with the name and the meaning of each character special character with examples.

Alternatively, you can use the String.substring() method.

# Replace the last character in a String using String.substring()

This is a three-step process:

  1. Use the String.substring() method to get a portion of the string up to 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 = 'bobbyhadz.com'; const replaced = str.substring(0, str.length - 1) + '_'; console.log(replaced); // ๐Ÿ‘‰๏ธ bobbyhadz.co_

replace last character in string using substring

The code for this article is available on GitHub

The String.substring() method returns a slice of the string from the start index to the excluding end index.

The method takes the following parameters:

NameDescription
start indexThe index of the first character to include in the returned substring
end indexThe index of the first character to exclude from the returned substring

You can also extract the logic into a reusable function.

index.js
const str = 'bobbyhadz.com'; function replaceLastCharacter(string, replacement) { return str.substring(0, str.length - 1) + replacement; } // ๐Ÿ‘‡๏ธ bobbyhadz.co_ console.log(replaceLastCharacter(str, '_')); // ๐Ÿ‘‡๏ธ bobbyhadz.co@ console.log(replaceLastCharacter(str, '@')); // ๐Ÿ‘‡๏ธ bobbyhadz.co!@#$ console.log(replaceLastCharacter(str, '!@#$'));
We used the String.substring() method in a similar way to how we used the String.slice() method to replace the last character in the string.

However, notice that we didn't use a negative end index in the call to String.substring().

There are a couple of differences between the String.substring() and the String.slice() methods:

  • The substring() method swaps its start and end index if the start index is greater than the end index. The slice() method returns an empty string in this case.
index.js
const str = 'bobby'; console.log(str.substring(3, 0)); // ๐Ÿ‘‰๏ธ bob console.log(str.slice(3, 0)); // ๐Ÿ‘‰๏ธ ''
  • If either of both arguments passed to substring() are negative, they are treated as if they were 0.
index.js
const str = 'bobby'; console.log(str.substring(-3)); // ๐Ÿ‘‰๏ธ bobby console.log(str.slice(-3)); // ๐Ÿ‘‰๏ธ bby

When given a negative index, the slice() method counts backward from the end of the string to find the indexes.

Which approach you pick is a matter of personal preference. I'd use te String.slice() method as it handles negative indices in a more predictable manner.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev