Last updated on Sep 24, 2020 by Suraj Sharma
In this tutorial, you will learn two ways to remove the last character of a JavaScript string
You can get the substring except the last character of the actual string by passing (0, string.length-1)
as arguments to the substring()
method
const string = 'JavaScript';
const newString = string.substring(0, string.length-1)
console.log(newString); // 'JavaScrip'
The slice()
method accepts negative numbers as its arguments.
So passing (0, -1)
to the slice()
method, returns a new string without the last character of the actual string.
const string = 'JavaScript';
const newString = string.slice(0, -1) // '-1' is equivalent to 'string.length-1'
console.log(newString); // 'JavaScrip'
Related Solutions
Rate this post
Suraj Sharma is the founder of Future Gen AI Services. He holds a B.Tech degree in Computer Science & Engineering from NIT Rourkela.