DEV Community

Cover image for 4 Ways to Remove the Last Character from a String in JavaScript 🚮
Gaël Thomas for HereWeCode

Posted on • Updated on • Originally published at herewecode.io

4 Ways to Remove the Last Character from a String in JavaScript 🚮

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.

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"
Enter fullscreen mode Exit fullscreen mode

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

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"
Enter fullscreen mode Exit fullscreen mode

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

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"
Enter fullscreen mode Exit fullscreen mode

If you want to have more information, you can read the slice documentation.

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).

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

console.log(newBookName)
// Output: "Atomic Habit"
Enter fullscreen mode Exit fullscreen mode

If you want to have more information, you can read the substring documentation.

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).

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

console.log(newBookName)
// Output: "Atomic Habit"
Enter fullscreen mode Exit fullscreen mode

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.

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.

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"
Enter fullscreen mode Exit fullscreen mode

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.


➡️ I'm starting to tweet more consistently. If you want to get more tips and resources about web programming -> Find me on my Twitter 🐦

Top comments (7)

Collapse
 
trevorzylks profile image
Trevor
const noLast = (book) => {
  const bookName = book
  splitIt= bookName.split(/\w$/)
  return splitIt[0]
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

I'd go with just:

const noLast = book=>book.split(/.$/)[0]
Enter fullscreen mode Exit fullscreen mode

Short and sweet 😊 - I also switched to . since we want to remove the last character (whatever it is)

Collapse
 
trevorzylks profile image
Trevor

A beautiful solution, friend!

Collapse
 
gaelgthomas profile image
Gaël Thomas

It's working well too! Thanks for sharing! 😊

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited
const bookName = 'Atomic Habits'
const newBookName = [...bookName].reduce((s,c,i,a)=>a.length-i-1?s+c:s)
Enter fullscreen mode Exit fullscreen mode
Collapse
 
gaelgthomas profile image
Gaël Thomas

It's working like a charm! ✨
However, I find it complex to read to do a simple operation. In my opinion, slice is the most straightforward way to remove the last character from a string in Javascript.

Collapse
 
arabelhousseyn profile image
elhousseyn arab

this one is not optimized
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)