JavaScript - How to Get the Last N Characters of a String? Last Updated : 26 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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. JavaScript const getChar = (s, n) => s.slice(-n); const s = "Hello World"; console.log(getChar(s, 5)); OutputWorld 2. Using substring() MethodThe substring() method calculates the starting index based on the string's length. JavaScript const getChars = (s, n) => s.substring(s.length - n); const s = "Hello World"; console.log(getChars(s, 5)); OutputWorld 3. Using Regular ExpressionsYou can use a regex pattern to extract the last N characters from a string. This approach is less common but effective in specific use cases. JavaScript const getChars = (s, n) => s.match(new RegExp(`.{${n}}$`))[0]; const s = "Hello World"; console.log(getChars(s, 5)); OutputWorld 4. Using Array.slice() After String SplitBy splitting the string into an array, you can use Array.slice() to extract the last N elements and join them back. JavaScript const getChars = (s, n) => s.split("").slice(-n).join(""); const s = "Hello World"; console.log(getChars(s, 5)); OutputWorld 5. Using substr() MethodAlthough substr() is deprecated, it can still be used in some environments. It allows extracting the last N characters using a negative start index. JavaScript const getChars = (s, n) => s.substr(-n); const s = "Hello World"; console.log(getChars(s, 5)); OutputWorld 6. Brute Force ApproachYou can also manually iterate over the string to construct the last N characters. This method is less efficient and mainly for educational purposes. JavaScript const getChars = (s, n) => { let res = ""; for (let i = s.length - n; i < s.length; i++) { res += s[i]; } return res; }; const s = "Hello World"; console.log(getChars(s, 5)); OutputWorld Which Approach Should You Use?ApproachWhen to Useslice()Best for simplicity and modern JavaScript.substring()Use when you prefer explicit index calculations.RegexIdeal when working with patterns or dynamic N values.Array.slice()Useful when working with array-like transformations.substr()Use only if supporting legacy environments.Brute ForceAvoid for real-world usage; use for educational purposes only. Comment More infoAdvertise with us Next Article JavaScript Program to Truncate a String to a Certain Length M mansigeekso9ii Follow Improve Article Tags : JavaScript Web Technologies javascript-string JavaScript-DSA JavaScript-Program +1 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 Last Characters from JS String 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 charact 2 min read JavaScript Program to Truncate a String to a Certain Length In this article, we are going to learn how can we truncate a string to a certain length. Truncating a string to a certain length in JavaScript means shortening a string so that it doesn't exceed a specified maximum length. This can be useful for displaying text in user interfaces, such as titles or 3 min read JavaScript Program for Generating a String of Specific Length In this article, we are going to discuss how can we generate a string having a specific length. You might encounter many situations when working with JavaScript where you need to produce a string with a specified length. This could be done for a number of reasons, including generating passwords, uni 4 min read JavaScript Program to Find Lexicographically Next String In this article, we are going to learn about Lexicographically next string in JavaScript. A lexicographically next string is the immediate string in a sorted order that comes after a given string when considering character sequences. It's determined by rearranging characters to find the smallest cha 3 min read JavaScript - How To Get The Last Caracter of a String? Here are the various approaches to get the last character of a String using JavaScript.1. Using charAt() Method (Most Common)The charAt() method retrieves the character at a specified index in a string. To get the last character, you pass the index str.length - 1.JavaScriptconst s = "JavaScript"; co 3 min read Like