# JavaScript Remove the Last Character From a String

> Remove the last character from a JavaScript string using slice(0, -1) or substring(0, string.length - 1), both explained with working code examples

**URL:** https://sentry.io/answers/how-do-i-remove-chop-slice-trim-off-last-character-in-string-using-javascript/

---

## The Problem

How can I remove the last character from a string in JavaScript?

## The Solution

Strings in JavaScript are [immutable](https://developer.mozilla.org/en-US/docs/Glossary/Immutable), so whenever we want to manipulate one, we must create a new string with our desired changes. Therefore, to remove the last character of a string, we must create a new string that excludes it. Two commonly used methods for this are [`slice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice) and [`substring`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring).

### Using `slice`

`slice` allows us to extract a substring by specifying inclusive start and end indices within the original string. Negative numbers are used to count backwards from the end of the string rather than forwards from the start, so `-1` will refer to the second-last character. Consider the following example:

```javascript
let myString = "Hello world!";

myString = myString.slice(0, -1);

console.log(myString); // will print "Hello world"
```

### Using `substring`

The `substring` method is similar to `slice` but does not allow us to specify negative indices. Therefore, we must provide the positive index of the end position by subtracting 1 from the length of the string. Like `slice`, the second argument is exclusive, so the returned substring stops just before the character at that index. Unlike `slice`, if the start index is greater than the end index, `substring` swaps the two arguments instead of returning an empty string.

```javascript
let myString = "Hello world!";

myString = myString.substring(0, myString.length - 1);

console.log(myString); // will print "Hello world"
```

### Comparison

The `slice` method is terser and its syntax will be familiar to Python programmers, but it may be initially confusing to programmers who have not previously encountered it. The `substring` method is more verbose but also more explicit.

---

*Source: [sentry.io/answers/how-do-i-remove-chop-slice-trim-off-last-character-in-string-using-javascript/](https://sentry.io/answers/how-do-i-remove-chop-slice-trim-off-last-character-in-string-using-javascript/)*
