How to usethe slice() Method in Javascript
In this approach, we will slice method for removing the last character of the string.The string.slice() is an inbuilt method in javascript that is used to return a part or slice of the given input string.
Syntax:
string.slice( startingIndex, endingIndex )
Example:
function removeCharacter(str) {
let newString = str.slice(0, -1);
return newString;
}
let str = "w3wiki";
console.log(removeCharacter(str));
Output
Geeksforgeek
JavaScript Program to Remove Last Character from the String
In this article, we will learn how to remove the last character from the string in JavaScript. The string is used to represent the sequence of characters. Now, we will remove the last character from this string.
Example:
Input : Geeks for geeks
Output : Geeks for geek
Input : w3wiki
Output : Geeksforgeek
Below are the following approaches through which we can remove the last character from the string:
Table of Content
- Approach 1: Using for loop
- Approach 2: Using the slice() Method
- Approach 3: Using substring() Method
- Approach 4: Using split() and join() Method
- Approach 5: Using Regular Expression