How to remove last letter of string - JavaScript

Mo Ibra

Mo Ibra

Dec 10, 2022

In this article we will learn how to remove last letter of a string.

It is very common to remove last letter of a string in JavaScript.

There is more than one way to solve this problem:

  • Using slice() method
  • Using subString() method

Problem

Now we have a problem, which is that we want to remove last letter of the string.

Imagine we have a string like this:

"Hello World"

And we want to remove the last letter so that the string becomes like this

"Hello Worl"

How to solve this problem?

As we said before, there's more than one way to solve this problem such as:

  • Using slice() method
  • Using subString() method

Remove last letter using slice method

We can use slice method to remove last letter of string.

Example

// String
const string = "Hello World";

// Remove last letter
const result = string.slice(0, -1);

// Result:
console.log(result);

Output

Hello Worl

The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end (Source: MDN)


Remove last letter using subString

We can use subString to remove last letter of a string as well.

Example

// String
const string = "Hello World";

// Remove last letter
const result = string.substring(0, string.length - 1);

// Result:
console.log(result);

Output

Hello Worl

The substring() method returns the part of the string from the start index up to and excluding the end index, or to the end of the string if no end index is supplied. (Source: MDN)


Thank you for reading

Thank you for reading my blog. 🚀

Suggestions For More Articles: