We will be using
substring() method to slice or trim off the last character from a string.
The slice() method helps us to remove or trim off a character from a string at a specific index. It do not change or modify the original string.
I have a start index and end index to specific which part of the string you want to extract.
var str = "Hello World"; var newStr = str.slice(0, -1); console.log(newStr)
Here the starting value is 0 and the end is
-1, because a negative index indicate an offset from the end of the string.
The negative index let us to extract / remove character from the end of the string.
If you give
-2, it will remove the last two characters from the end of the given string.
It does not change or modify the original string.
IMPORTANT : The character in the startIndex is always included and the character in the endIndex is excluded while extracting the string.
var str = "Hello World"; var newStr = str.substring(0, str.length-1); console.log(newStr)
Here the start index is
0 and the end index is
str.length-1, and anything between these two points is the extracted string.