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:

Javascript
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

Similar Reads

Approach 1: Using for loop

In this approach, we will use a brute force approach for removing the character from the string. We run the loop through the string and iterate over all the characters except the last character. Now, return the modified string....

Approach 2: Using the slice() Method

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....

Approach 3: Using substring() Method

In this approach, we will use substring() method for removing last character of string. The string.substring() is an inbuilt function in JavaScript that is used to return the part of the given string from the start index to the end index. Indexing start from zero (0)....

Approach 4: Using split() and join() Method

In this approach, we will split the string and then we use pop() method for removing the last character and then we will use join() method for joining the array back....

Approach 5: Using Regular Expression

The regular expression approach uses replace() with a regex pattern targeting the last character (.$) and replacing it with an empty string. This effectively removes the last character from the string, providing a concise solution....