4 Ways to Remove the Last Character from a String in JavaScript 🚮
Tags: javascript, webdev, beginners, codenewbie
Published: 2021-08-21 08:14:49

A short tutorial on how to get and remove the last character of string in JavaScript.

## Remove the last character from String using Slice

The most common way to trim the last character is by using the JavaScript `slice` method. This method can take up to two indexes as parameters and get the string between these two values.

To keep the whole string and remove the last character, you can set the first parameter to `0` and pass the string length - 1 as the second parameter.

```javascript
const bookName = 'Atomic Habits' // 13 characters (indexes between 0 and 12)
const newBookName = bookName.slice(0, bookName.length - 1) // Between (0 and 12)

console.log(newBookName)
// Output: "Atomic Habit"
```

As a shortcut, if you pass `-1` as the second parameter, it will automatically calculate the string length - 1 for you.

```javascript
const bookName = 'Atomic Habits' // 13 characters (indexes between 0 and 12)
const newBookName = bookName.slice(0, -1) // Between (0 and 12)

console.log(newBookName)
// Output: "Atomic Habit"
```

If you want to remove two characters at the end, you can do as follow and so on!

```javascript
const bookName = 'Atomic Habits' // 13 characters (indexes between 0 and 12)
const newBookName = bookName.slice(0, -2) // Between (0 and 11)

console.log(newBookName)
// Output: "Atomic Habi"
```

If you want to have more information, you can read the [slice documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice).

## Remove the last character from String using Substring or SubStr

### Substring method

Another way to delete the last character of a string is by using the `substring` method. This method will extract characters from a string. It takes as a first parameter the index where you want to start, and as a second, the index where you want to stop.

The idea here is to start from `0` and keep all the letters except the last one. To do that, we will use the length of the string - 1 (index of the last position).

```javascript
const bookName = 'Atomic Habits'
const newBookName = bookName.substring(0, bookName.length - 1)

console.log(newBookName)
// Output: "Atomic Habit"
```

If you want to have more information, you can read the [substring documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring).

### Substr method

You can also use the `substr` method. This method will extract characters from a string. It takes as a first parameter the starting index, and as a second, the number of characters you want to extract.

Here, we want to start from zero and extract all the characters except the last one. We can still use the string length - 1 as a value (from index 0 extracts 10 characters).

```javascript
const bookName = 'Atomic Habits'
const newBookName = bookName.substr(0, bookName.length - 1)

console.log(newBookName)
// Output: "Atomic Habit"
```

> **Note:** The only difference between `subtring` and `substr` is the extraction process. `substring` uses two indexes while `substr` uses one index and the number of characters to extract.

If you want to have more information, you can read the [substr documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr).

## Remove the last character from String using Pop

If you want to trim the last character of your string, you can also make an array conversion using `split`. Thanks to that, we will use the built-in array functions to remove the last element. Then, we will convert again to a string.

```javascript
const bookName = 'Atomic Habits'

// We create an array: ['A', 't', 'o', 'm', 'i', 'c', ' ', 'H', 'a', 'b', 'i', 't', 's']
const bookNameArray = bookName.split(''
// We delete the last element (letter 's')
bookNameArray.pop()

// We convert back the array to a string
const newBookName = bookNameArray.join('')

console.log(newBookName)
// Output: "Atomic Habit"
```

**What's next?**

It's over! 🎉 Now you know how to remove the last character from a string in JavaScript, discover my other article about [how to remove the first character from a string in JavaScript](https://herewecode.io/blog/javascript-remove-first-character-string/).

---

➡️ I'm starting to tweet more consistently. If you want to get more tips and resources about web programming -> [Find me on my Twitter 🐦](https://twitter.com/gaelgthomas/)