Blog.

How to remove the last character from string in Javascript

Cover Image for How to remove the last character from string in Javascript

Using slice() The easiest way to remove the last character of a string in JavaScript is by using slice method

 

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

Daniel Turuș

@danielturus

Hi! My name is Daniel and I am a Full-stack JavaScript developer.

If you like my material, please consider following me on Twitter to get notified when new posts are published, ask me a question and stay in touch.