Difference between substr() and substring() in JavaScript Last Updated : 08 Jan, 2024 Comments Improve Suggest changes Like Article Like Report In JavaScript Both of the functions are used to get the specified part of the string, But there is a slight difference between them. substr() and substring() are string methods used to extract parts of a string. The main difference is that substr() accepts a length parameter, while substring() accepts start and end indices. JavaScript str.substr() FunctionThe str.substr() function returns the specified number of characters from the specified index from the given string. Syntax:str.substr(start, len);Example: This example uses only one parameter for both functions and produces the same output. JavaScript const str1 = "GeeksForGeeks"; const substrResult = str1.substr(7); const substringResult = str1.substring(7); console.log("Str.substr(7) =", substrResult); console.log("Str.substring(7) =", substringResult); OutputStr.substr(7) = rGeeks Str.substring(7) = rGeeks JavaScript str.substring() FunctionThis function gets the characters from a string, between two specified indices, and returns the new string. Syntax:str.substring(start, end);Example: This example uses the parameter (3, 7) for both functions and returns the output. JavaScript const str1 = "GeeksForGeeks"; const substrResult = str1.substr(3, 7); const substringResult = str1.substring(3, 7); console.log("Str.substr(3, 7) =", substrResult); console.log("Str.substring(3, 7) =", substringResult); OutputStr.substr(3, 7) = ksForGe Str.substring(3, 7) = ksFo Difference between substr() and substring()Featuresubstr()substring()Syntaxstr.substr(start, length)str.substring(start, end)Parametersstart: Starting indexstart: Starting indexLengthlength: Number of characters to extractend: Ending index (exclusive)Negative IndexAccepts negative start (counts from the end)Does not accept negative start or endBehavior with Negative IndexIf start is negative, it's treated as str.length + start. If length is negative, it's ignored.If either start or end is negative, they are treated as 0.Handling Omitted ParametersIf length is omitted, extracts characters to the end of the string.If end is omitted, extracts characters to the end of the string. Comment More infoAdvertise with us Next Article Difference between substr() and substring() in JavaScript P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies javascript-string JavaScript-Questions Similar Reads Difference between String.slice and String.substring in JavaScript These 2 functions are quite similar in their Syntax But are different in some cases. Let's see the difference between them. JavaScript slice() Method:This method selects the part of a string and returns the selected part as a new string. Start and end parameters are used to specify the extracted par 3 min read What is the difference between Array.slice() and Array.splice() in JavaScript ? In JavaScript, slice() and splice() are array methods with distinct purposes. `slice()` creates a new array containing selected elements from the original, while `splice()` modifies the original array by adding, removing, or replacing elements. slice():The slice() method in JavaScript extracts a sec 3 min read Check for Substring in JavaScript Given two strings, check if one string is substring of another."bce" is substring of "abcde""ae" is not substring of "abcde"Empty String is a substring of all stringsUsing includes() - Most used and Simplest MethodThe includes() method checks whether a string contains a substring. JavaScriptlet s = 2 min read Check If An Array of Strings Contains a Substring in JavaScript Here are the different ways to check if an array of strings contains a substring in JavaScript1. Using includes() and filter() methodWe will create an array of strings, then use includes() and filter() methods to check whether the array elements contain a sub-string.JavaScriptconst a = ['Geeks', 'gf 4 min read Are String Objects in JavaScript? Strings in JavaScript are one of the primitive data types, alongside numbers, booleans, null, undefined, bigint, and symbols. Unlike objects, primitives do not have methods or properties of their own. However, when you attempt to call a method or access a property on a string, JavaScript automatical 2 min read How are strings stored in JavaScript ? In this article, we will try to understand how strings are stored in JavaScript. Strings are a primitive data type in JavaScript and are allocated a special place in memory to store and manipulate. Unlike some other languages, JavaScript does not have a String Constant Pool. Instead, JavaScript engi 3 min read Remove a Character From String in JavaScript In JavaScript, a string is a group of characters. Strings are commonly used to store and manipulate text data in JavaScript programs, and removing certain characters is often needed for tasks like:Removing unwanted symbols or spaces.Keeping only the necessary characters.Formatting the text.Methods t 3 min read JavaScript - Insert Character in JS String In JavaScript, characters can be inserted at the beginning, end, or any specific position in a string. JavaScript provides several methods to perform these operations efficiently.At the BeginningTo insert a character at the beginning of a string, you can use string concatenation or template literals 1 min read JavaScript - Delete Character from JS String In JavaScript, characters can be deleted from the beginning, end, or any specific position in a string. JavaScript provides several methods to perform these operations efficiently.Delete First CharacterTo remove the first character from a string, we can use methods like slice, substring, or regular 2 min read JavaScript - Delete First Character of a String To delete the first character of a string in JavaScript, you can use several methods. Here are some of the most common onesUsing slice()The slice() method is frequently used to remove the first character by returning a new string from index 1 to the end.JavaScriptlet s1 = "GeeksforGeeks"; let s2 = s 1 min read Like