How to Truncate a String in JavaScript ? Last Updated : 27 Jun, 2024 Comments Improve Suggest changes Like Article Like Report In JavaScript, there are several ways to truncate a string which means cutting off a part of the string to limit its length. Truncating a string is useful when we want to display only a certain number of the characters in the user interfaces such as previewing a longer text or ensuring that text fits within the specified space.Use the below methods to Truncate a String in JavaScript:Table of ContentUsing the substring() methodUsing the slice() methodUsing Regular ExpressionUsing the substring() methodIn this approach, we are using the substring() method to extract characters from the string between the two specified indices and return the new sub-string.Example: Truncate a String in JavaScript using the substring() method. JavaScript function GFG(str, maxLength) { if (str.length > maxLength) { return str.substring(0, maxLength) + '...'; } return str; } const longText = "GeeksforGeeks, Learning."; const truncatedText = GFG(longText, 20); console.log(truncatedText); OutputGeeksforGeeks, Learn... Using the slice() methodIn this approach, we are using the slice() method that extracts a section of the string and returns it as a new string without the modifying the original string.Example: Truncate a String in JavaScript using the slice() method. JavaScript function GFG(str, maxLength) { if (str.length > maxLength) { return str.slice(0, maxLength) + '...'; } return str; } const longText = "GeeksforGeeks , Learning."; const truncatedText = GFG(longText, 20); console.log(truncatedText); OutputGeeksforGeeks , Lear... Using Regular ExpressionIn this approach, we use a regular expression to match the desired number of characters from the beginning of the string. This method can be particularly useful if you want to ensure that the truncation does not break words in the middle.Example: Truncate a String in JavaScript using a regular expression. JavaScript function truncateString(str, maxLength) { if (str.length <= maxLength) { return str; } const regex = new RegExp(`^.{0,${maxLength}}`); const truncated = str.match(regex)[0]; return truncated; } // Example usage const str = "This is a long string that needs to be truncated."; const maxLength = 20; const result = truncateString(str, maxLength); console.log(result); // Output: "This is a long stri" OutputThis is a long strin Comment More infoAdvertise with us Next Article How to Truncate a String in JavaScript ? S subramanyasmgm Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to Truncate an Array in JavaScript? Here are the different methods to truncate an array in JavaScript1. Using length PropertyIn Array.length property, you can alter the length of the array. It helps you to decide the length up to which you want the array elements to appear in the output.JavaScriptconst n = [1, 2, 3, 4, 5, 6]; n.length 4 min read How to modify a string in JavaScript ? JavaScript strings are used for storing and manipulating text. It can contain zero or more characters within quotes and its indexing starts with 0. Strings are defined as an array of characters. In Javascript, we can also convert strings to a Character array. Representing a String: 1. Using double 3 min read Convert Array to String in JavaScript In JavaScript, converting an array to a string involves combining its elements into a single text output, often separated by a specified delimiter. This is useful for displaying array contents in a readable format or when storing data as a single string. The process can be customized to use differen 7 min read How to replace all dots in a string using JavaScript ? We will replace all dots in a string using JavaScript. There are multiple approaches to manipulating a string in JavaScript. Table of Content Using JavaScript replace() MethodUsing JavaScript Split() and Join() Method Using JavaSccript reduce() Method and spread operatorUsing JavaScript replaceAll() 4 min read JavaScript - How to Count Words of a String? Here are the various methods to count words of a string in JavaScript.1. Using split() MethodThe simplest and most common approach is to use the split() method to divide the string into an array of words and count the length of the array.JavaScriptconst count = (s) => s.trim().split(/\s+/).length 3 min read Like