How to Remove the Last Character of a String in JavaScript?

To remove the last character of a string in JavaScript, you can use the String.slice() method. The slice() method takes two arguments, the starting and ending index and returns a new string which is sliced from the original string.

The main advantage of the slice() method is that it supports negative indexing which means that you don’t have to calculate the length of the string to remove the last character from it instead you can pass -1 as its second parameter.

Passing -1 as the end index to the slice() method represents that the slicing will start from the end of the string.

See the following example:

// Original string
const str = 'Welcome';

// Remove last character of string
const slicedStr = str.slice(0, -1);

console.log(slicedStr);
// Output: 👉 "Welcom"

You can also pass the last index of the string as a second parameter to the slice() method to remove the last character from it.

See this example:

// Original string
const str = 'Welcome';

// Remove last character of string
const slicedStr = str.slice(0, str.length - 1);

console.log(slicedStr);
// Output: 👉 "Welcom"

2. Using the substring() Method

The substring() method also takes the starting and ending index of the string to be sliced from the original string. It basically returns a substring from the original string that’s why named as substring().

But like the slice() method, it does not support negative indexing. It means that you have to pass the last index of the original string(instead of passing -1) to remove the last character from it.

// Original string
const str = 'Welcome';

// Remove last character of string
const slicedStr = str.substring(0, str.length - 1);

console.log(slicedStr);
// Output: 👉 "Welcom"

3. Using the replace() Method

This is an advanced way of removing the last character from a string. In this method we use the String.replace() method which returns a new string by replacing all the matches in the original string with the replacement string we pass to it as a second argument.

As we want to remove only the last character from the string, therefore, we have to pass a regular expression which matches only the last character in the string and then replace it with an empty string.

See this example:

// Original string
const str = 'Welcome';

// Remove last character of string
const slicedStr = str.replace(/.$/, '');

console.log(slicedStr);
// Output: 👉 "Welcom"

In the above example, we have used a the regular expression /.$/. In this regular expression, the . matches any character within the string and the $ represents the end of the string.

So if we combine both characters that is .$, it represents only the last character of the string.

The replace() method finds this match and replaces it with an empty string("") to remove the last character from the string. That’s how it works.

I hope you find this article helpful. Thanks for reading.

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.

    View all posts

Leave a Comment