JavaScript- Remove Last Characters from JS String Last Updated : 21 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report These are the following ways to remove first and last characters from a String:1. Using String slice() MethodThe slice() method returns a part of a string by specifying the start and end indices. To remove the last character, we slice the string from the start (index 0) to the second-to-last character. JavaScript let str = "Hello GFG"; let res = str.slice(0, -1); console.log(res); OutputHello GF 2. Using String substring() MethodThe substring() method extracts the part of a string between two specified indices. To remove the last character, provide 0 as the starting index and the second-to-last index as the ending index. JavaScript let str = "Hello GFG"; let res = str.substring(0, str.length - 1); console.log(res); OutputHello GF 3. Using String substr() MethodThe substr() method returns a portion of the string starting from a specified position for a given length. Use it to remove the last character by specifying the starting index and reducing the length by 1. JavaScript let str = "Hello GFG"; let res = str.substr(0, str.length - 1); console.log(res); OutputHello GF 4. Using Array MethodsConvert the string into an array, remove the last element using the pop() method, and join the array back into a string. JavaScript let str = "Hello GFG"; let arr = str.split(""); arr.pop(); let res = arr.join(""); console.log(res); OutputHello GF 5. Using Regular Expression with replace()Use a regular expression to match and remove the last character of the string. JavaScript let str = "Hello GFG"; let res = str.replace(/.$/, ""); console.log(res); OutputHello GF 6. Using String replace() Without Regular ExpressionsManually specify the last character to be replaced with an empty string using replace() method. JavaScript let str = "Hello GFG"; let res = str.replace("G", ""); // Replace the last character console.log(res); OutputHello FG 7. Using String Destructuring and Array slice()Destructure the string into an array of characters, use slice() to remove the last character, and join it back into a string. JavaScript let str = "Hello GFG"; let result = [...str].slice(0, -1).join(""); console.log(result); OutputHello GF 8. Using String TrimmingIn some cases where the last character is a known whitespace or unwanted character, the trim() method can be used. JavaScript let str = "Hello GFG "; let res = str.trimEnd(); console.log(res); OutputHello GFG Comment More infoAdvertise with us Next Article JavaScript Program to Remove Last Character from the String B blalverma92 Follow Improve Article Tags : JavaScript Web Technologies Geeks Premier League javascript-string JavaScript-Program Geeks Premier League 2023 +2 More Similar Reads 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 : Geeksforgeeks Output : 3 min read JavaScript - Remove all Occurrences of a Character in JS String These are the following ways to remove all occurrence of a character from a given string: 1. Using Regular ExpressionUsing a regular expression, we create a pattern to match all occurrences of a specific character in a string and replace them with an empty string, effectively removing that character 2 min read JavaScript - How to Get the Last N Characters of a String? Here are the different methods to get the last N characters of a string in JavaScript.1. Using slice() MethodThe slice() method is the most commonly used approach for extracting the last N characters, thanks to its simplicity and support for negative indices.JavaScriptconst getChar = (s, n) => s. 2 min read JavaScript - How to Replace Multiple Characters in a String? Here are the various methods to replace multiple characters in a string in JavaScript.1. Using replace() Method with Regular ExpressionThe replace() method with a regular expression is a simple and efficient way to replace multiple characters.JavaScriptconst s1 = "hello world!"; const s2 = s1.replac 3 min read JavaScript Program to Remove Consecutive Duplicate Characters From a String We are going to implement a JavaScript program to remove consecutive duplicate characters from a string. In this program, we will eliminate all the consecutive occurrences of the same character from a string.Example:Input: string: "geeks" Output: "geks"Explanation :consecutive "e" should be removedT 5 min read JavaScript Program to Remove Vowels from a String The task is to write a JavaScript program that takes a string as input and returns the same string with all vowels removed. This means any occurrence of 'a', 'e', 'i', 'o', 'u' (both uppercase and lowercase) should be eliminated from the string. Given a string, remove the vowels from the string and 2 min read Like