Using slice()
The easiest way to remove the last character of a string in JavaScript is by using slice method
const name = 'Robert'
console.log(name.slice(0, -1)) // Robert
The slice
method returns a new string that contains the extracted portion of the original string,
so it does not mutate, modify the original string (in our case const name
will remain unchanged no matter how many times we run name.slice(0, -1)
method)
Using substring()
const fruit = 'Banana'
console.log(fruit.substring(0, fruit.length - 1)) // Banana