How to delete last symbol in JS string using
slice returns a part of the string. We pass in two values,
slice returns us a substring of
end is a negative number, the lookup begins from the end of the string.
Is the same as
base.slice(0, base.length - 1);
substring to remove the last element from a string in JS
As the function
slice only returns us a substring and doesn’t mutate the original string, we can use the function
substring to get the same outcome.
Take a note, though, that if you pass a negative number into
substring as a second argument, you’ll get an empty string
base.substring(0, -1); // ''
The behavior is different from
slice with regards to negative indicies.
- If you need to delete the last char of the string in JS, you should create a new string
- You can use either