JavaScript- Remove Last Characters from JS String Last Updated : 21 Nov, 2024 Comments Improve Suggest changes 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- Remove Last Characters from JS 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 JavaScript - Extract First Word from a String Here are the different methods to extract the first word from a stringUsing split() Method - Most PopularThe split() method splits the string into an array of words based on spaces, and the first element of this array is the first word.JavaScriptconst s1 = "Hello World, welcome to GeeksforGeeks"; co 2 min read JavaScript Program to Swap Characters in a String In this article, We'll explore different approaches, understand the underlying concepts of how to manipulate strings in JavaScript, and perform character swaps efficiently. There are different approaches for swapping characters in a String in JavaScript: Table of Content Using Array ManipulationUsin 6 min read How to Convert Char to String in JavaScript ? In the article, we are going to learn about conversion Char to a string by using JavaScript, Converting a character to a string in JavaScript involves treating the character as a string, a character is a single symbol or letter, while a string is a sequence of characters. Strings can contain multipl 3 min read JavaScript Program to Access Individual Characters in a String In this article, we are going to learn about accessing individual characters from the given string. Accessing individual characters in a string means retrieving a specific character from the string using its index position (zero-based) or iterating through each character one by one. Example: Input : 4 min read Like