JavaScript - How to Get the Last N Characters of a String? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 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- Remove Last Characters from JS String 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 to Find the Index of the Last Occurrence of a Substring in a String Finding the index of the last occurrence of a substring in a string is a common task in JavaScript. The last occurrence of a substring in a string in JavaScript refers to finding the final position where a specified substring appears within a given string. We may want to know the position of the las 5 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 How to get the last n characters of a PHP string? Write a PHP program to get last n characters of a given string. Examples: Input : $str = "GeeksforGeeks!" $n = 6 Output : Geeks! Input : $str = "GeeksforGeeks!" $n = 9 Output : forGeeks! Method 1: Tn this method, traverse the last N characters of the string and keep appending them in a new string. E 1 min read Like