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

In this article, we're going to show you how to remove the last character of a string no matter if it's a simple comma, a newline character or even a complex unicode character.

Using substring()

We can simply use substring() to get the entire string except for the last character like so:

const str = 'foo,bar,baz,';
const newStr = str.substr(0, str.length - 1);

console.log(newStr); // output: 'foo,bar,baz'

In the example above, you can see that we specify the start position at 0 and the end position at the length of the string except one character at the end. This helps us get the string without the last character.

Using slice()

With slice() we can extract all characters of a string except the last character like so:

const newStr = str.slice(0, -1);

console.log(newStr); // output: 'foo,bar,baz'

In the example above, you can see that we specify the start position as 0 and the ending position as -1. Specifying negative positions in slice() means that they're relative to the end of the string. Therefore, we get the entire string with the last character sliced off.

Splitting the String and Popping or Slicing the Last Character

We can split the string at each character to get an array of all characters in the string. If we simply pop the last character from that array of characters and then join all the characters back into a string, we get the same string but without the last character. For example:

const strArr = str.split('');
strArr.pop();

console.log(strArr.join('')); // output: 'foo,bar,baz'

Or, alternatively, we can use Array.prototype.slice():

console.log(str.split('').slice(0, -1).join('')); // output: 'foo,bar,baz'

Keep in mind that this method might be slower than others as it involves three function calls.

What If the Last Character Is a Unicode Character?

Most modern JavaScript engines encode strings as UTF-16. This means that string functions such as substring(), slice() and split() would work fine with characters represented by one "code point" (i.e. a single slot in unicode that represents a character). This includes common international languages. Consider, for example, the following:

const str = 'groß';

// using substring
console.log(str.substring(0, str.length - 1)); // output: 'gro'

// using slice
console.log(str.slice(0, -1)); // output: 'gro'

// using split (or pop)
console.log(str.split('').slice(0, -1).join('')); // output: 'gro'

As you can see from the examples above, everything works as expected. However, if the last character had a higher unicode code-point, it would be stored as a "surrogate pair" (which is a way of encoding unicode characters with high code-points in the UTF-16 encoding scheme). This would mean that we won't get the expected result. For example:

const str = 'foo🦁';

// using substring
console.log(str.substring(0, str.length - 1)); // output: 'foo�'

// using slice
console.log(str.slice(0, -1)); // output: 'foo�'

// using split (or pop)
console.log(str.split('').slice(0, -1).join('')); // output: 'foo�'

As you can see from the examples above, the string functions don't work as expected on strings with higher unicode code-points. For those kind of strings we can do either of the following:

1. Convert String to an Array Using Unicode-Aware Function and Pop/Slice Last Character:

The spread operator and Array.from(), both are unicode-aware since they utilize the Symbol.iterator of an object (i.e. they use the default iterator of an object). In the case of string, the Symbol.iterator provides an iterator that iterates over the code points of a string value. It returns each code point as a string value. For example:

const str = 'foo🦁';

console.log(Array.from(str).slice(0, -1).join('')); // output: 'foo'

// or:
console.log([ ...str ].slice(0, -1).join('')); // output: 'foo'

You can, of course, use pop() instead of slice() here as well.

2. Using a Regular Expression With the u Flag:

ES6 added support for unicode-aware regular expressions using the u flag. We can use it to remove the last character from a string like so:

const str = 'foo🦁';

console.log(str.replace(/.$/u, '')); // output: 'foo'

What If the Last Character Is a Newline Character?

There might be situations where the last character we want to remove is actually a newline character. We can do so using regular expressions with the s flag like so:

const str = 'foo\n';

console.log(str.replace(/.$/s, '')); // output: 'foo'

The "s" flag is used to indicate that the special dot character (".") should additionally match the following new line characters in a string (which it would not match otherwise):

  • U+000A Line Feed (LF) ("\n");
  • U+000D Carriage Return (CR) ("\r");
  • U+2028 Line Separator;
  • U+2029 Paragraph Separator.

This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.