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 …


This content originally appeared on DEV Community and was authored by Gaël Thomas

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"

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"

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"

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"

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"

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"

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 ?


This content originally appeared on DEV Community and was authored by Gaël Thomas


Print Share Comment Cite Upload Translate Updates
APA

Gaël Thomas | Sciencx (2021-08-21T08:14:49+00:00) 4 Ways to Remove the Last Character from a String in JavaScript ?. Retrieved from https://www.scien.cx/2021/08/21/4-ways-to-remove-the-last-character-from-a-string-in-javascript-%f0%9f%9a%ae/

MLA
" » 4 Ways to Remove the Last Character from a String in JavaScript ?." Gaël Thomas | Sciencx - Saturday August 21, 2021, https://www.scien.cx/2021/08/21/4-ways-to-remove-the-last-character-from-a-string-in-javascript-%f0%9f%9a%ae/
HARVARD
Gaël Thomas | Sciencx Saturday August 21, 2021 » 4 Ways to Remove the Last Character from a String in JavaScript ?., viewed ,<https://www.scien.cx/2021/08/21/4-ways-to-remove-the-last-character-from-a-string-in-javascript-%f0%9f%9a%ae/>
VANCOUVER
Gaël Thomas | Sciencx - » 4 Ways to Remove the Last Character from a String in JavaScript ?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/21/4-ways-to-remove-the-last-character-from-a-string-in-javascript-%f0%9f%9a%ae/
CHICAGO
" » 4 Ways to Remove the Last Character from a String in JavaScript ?." Gaël Thomas | Sciencx - Accessed . https://www.scien.cx/2021/08/21/4-ways-to-remove-the-last-character-from-a-string-in-javascript-%f0%9f%9a%ae/
IEEE
" » 4 Ways to Remove the Last Character from a String in JavaScript ?." Gaël Thomas | Sciencx [Online]. Available: https://www.scien.cx/2021/08/21/4-ways-to-remove-the-last-character-from-a-string-in-javascript-%f0%9f%9a%ae/. [Accessed: ]
rf:citation
» 4 Ways to Remove the Last Character from a String in JavaScript ? | Gaël Thomas | Sciencx | https://www.scien.cx/2021/08/21/4-ways-to-remove-the-last-character-from-a-string-in-javascript-%f0%9f%9a%ae/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.