You're confusing substring() and substr(): substring() expects two indices and not offset and length. In your case, the indices are 5 and 2, ie characters 2..4 will be returned as the higher index is excluded.
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › substring
String.prototype.substring() - JavaScript | MDN
The substring() method of String values returns the part of this string from the start index up to and excluding the end index, or to the end of the string if no end index is supplied.
W3Schools
w3schools.com › jsref › jsref_substring.asp
JavaScript String substring() Method
The substring() method extracts characters, between two indices (positions), from a string, and returns the substring.
Videos
00:57
substring() in Javascript 🔥 #javascript #DSA #javascriptinterview ...
07:04
JavaScript Substring Method in 5 Minutes - YouTube
06:08
How to use Substrings in Javascript - YouTube
09:26
#76 The substring() & substr() Method | Array & String Methods ...
08:02
Learn JavaScript STRING SLICING in 8 minutes! ✂️ - YouTube
06:32
Master Substring Searches in JavaScript! Quick & Easy Tutorial” ...
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › substr
String.prototype.substr() - JavaScript | MDN
The substr() method of String values returns a portion of this string, starting at the specified index and extending for a given number of characters afterwards.
Mimo
mimo.org › glossary › javascript › substring
JavaScript Substring Method: Learn String Extraction
The substring() method in JavaScript extracts a portion of a string and returns it as a new string without modifying the original string.
Top answer 1 of 6
105
You're confusing substring() and substr(): substring() expects two indices and not offset and length. In your case, the indices are 5 and 2, ie characters 2..4 will be returned as the higher index is excluded.
2 of 6
67
You have three options in Javascript:
//slice
//syntax: string.slice(start [, stop])
"Good news, everyone!".slice(5,9); // extracts 'news'
//substring
//syntax: string.substring(start [, stop])
"Good news, everyone!".substring(5,9); // extracts 'news'
//substr
//syntax: string.substr(start [, length])
"Good news, everyone!".substr(5,4); // extracts 'news'
Bennadel
bennadel.com › blog › 2159-using-slice-substring-and-substr-in-javascript.htm
Using Slice(), Substring(), And Substr() In Javascript
April 21, 2020 - If it is not provided, the substring will consist of the start index all the way through the end of the string. For both the slice() and substring() methods, the second argument is exclusive; that is, the resultant substring will not contain the character at the final index. Let's take a look as these three approaches in action: <!DOCTYPE html> <html> <head> <title>Extracting Substrings In Javascript</title> <script type="text/javascript"> // For these demos, let's create a numbered string so that // we can easily see where the indexes come into play with // all of the substring methods.
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-string-substring-method
JavaScript String substring() Method - GeeksforGeeks
If only the starting index is provided, substring() will return the substring from that index to the end of the string. ... Like most string methods in JavaScript, substring() does not alter the original string.
Published January 16, 2026
Medium
medium.com › @natarajanck2 › ️-substring-in-javascript-clean-cuts-from-your-strings-37d39fd34599
️ Substring in JavaScript: Clean Cuts from Your Strings
Sitemap · Open in app · Sign up · Sign in · Get app · Write · Search
TechOnTheNet
techonthenet.com › js › string_substring.php
JavaScript: String substring() method
In JavaScript, substring() is a string method that is used to extract a substring from a string, given start and end positions within the string.
GeeksforGeeks
geeksforgeeks.org › javascript › difference-between-substr-and-substring-in-javascript
Difference between substr() and substring() in JavaScript - GeeksforGeeks
July 11, 2025 - const str1 = "GeeksForGeeks"; const ...tr.substring(7) =", substringResult); ... This function gets the characters from a string, between two specified indices, and returns the new string....
Stack Abuse
stackabuse.com › bytes › difference-between-substr-and-substring-in-javascript
Difference Between substr() and substring() in JavaScript
September 13, 2023 - The substr() method in JavaScript is used to extract parts of a string, starting from the index specified and extending for a given number of characters afterwards.
Droidscript
droidscript.org › javascript › Global_Objects › String › substring().html
str.substring()
JavaScript Reference · Contents · Basics · Statements · Operators · Global Objects · The substring() method returns a subset of a string between one index and another, or through the end of the string. str.substring(indexStart[, indexEnd]) indexStart ·
W3Schools
w3schools.com › jsref › jsref_indexof.asp
JavaScript String indexOf() Method
break class const continue debugger do...while for for...in for...of function if...else let return switch throw try...catch var while JS Strings · at() charAt() charCodeAt() codePointAt() concat() constructor endsWith() fromCharCode() includes() indexOf() isWellFormed() lastIndexOf() length localeCompare() match() matchAll() padEnd() padStart() prototype repeat() replace() replaceAll() search() slice() split() startsWith() substr() substring() toLocaleLowerCase() toLocaleUpperCase() toLowerCase() toString() toUpperCase() toWellFormed() trim() trimEnd() trimStart() valueOf() JS Typed Arrays
W3Schools
w3schools.com › jsref › jsref_includes.asp
JavaScript String includes() Method
break class const continue debugger do...while for for...in for...of function if...else let return switch throw try...catch var while JS Strings · at() charAt() charCodeAt() codePointAt() concat() constructor endsWith() fromCharCode() includes() indexOf() isWellFormed() lastIndexOf() length localeCompare() match() matchAll() padEnd() padStart() prototype repeat() replace() replaceAll() search() slice() split() startsWith() substr() substring() toLocaleLowerCase() toLocaleUpperCase() toLowerCase() toString() toUpperCase() toWellFormed() trim() trimEnd() trimStart() valueOf() JS Typed Arrays
Oodlestechnologies
oodlestechnologies.com › blogs › how-to-work-substring-method-in-javascript
How to work Substring method in JavaScript
February 14, 2020 - Now, the index position within string starts with 0 that means at index position 0 we have this letter 'J' and at index position 2 we have this letter 'a' and so on. If the value of the start parameter is greater than the value of the end parameter, this method will swap the two arguments. This means start will be used as end and end will be used as a start. var str = "JavaScript Blog" var result = str.substring(10, 0); console.log(result) // JavaScript