How to delete the last character of a string in JavaScript

How to delete the last character from a string?

The simplest solution is to useslice()String method, passing 2 parameters. The first is 0, which is the starting point. The second is the number of items to be deleted. Passing a negative number will be deleted from the end. This is the solution:

const text = 'abcdef'
const editedText = text.slice(0, -1) //'abcde'

Please noteslice()The method does not modify the original string.

It creates a new string, which is why it was assigned to the new variable in the example above.

Download mine for freeJavaScript beginner's manual

More js tutorials: