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.

Answer from Christoph on Stack Overflow
🌐
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.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › substr
String.prototype.substr() - JavaScript | MDN
2 weeks ago - 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.
🌐
freeCodeCamp
freecodecamp.org › news › javascript-substring-examples-slice-substr-and-substring-methods-in-js
JavaScript Substring Examples - Slice, Substr, and Substring Methods in JS
March 22, 2020 - This method basically gets a part of the original string and returns it as a new string. The substring method expects two parameters: ... Let’s see the usage in an example. Suppose that we have the example string below: const myString = "I am learning JavaScript and it is cool!";
🌐
Refine
refine.dev › home › blog › tutorials › javascript substring method
JavaScript Substring Method | Refine
January 1, 2025 - JavaScript substring() is a String method that is typically used to extract and store part of a string.
🌐
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.
Find elsewhere
🌐
YouTube
youtube.com › james q quick
JavaScript Substring Method in 5 Minutes - YouTube
Learn JavaScript String methods in 5 minutes. In this video, we'll use the substring method to parse out pieces of a JavaScript string by defining start and ...
Published: May 3, 2022
Views: 10K
🌐
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
🌐
Edureka
edureka.co › blog › javascript-substring
JavaScript Substring() | How is it different from substr() & slice()? | Edureka
January 9, 2025 - The substring() is an inbuilt function in JavaScript which returns a part of the given string from start index to end index. The indexing starts from zero.
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › substring
Substring vs Substr vs Slice in JavaScript - Mastering JS
June 20, 2019 - The substring() function is the most common way to get a substring in JavaScript. It takes two parameters: indexStart and indexEnd. It returns the portion of the string that starts at indexStart and ends the character immediately preceding indexEnd.
🌐
Medium
medium.com › nerd-for-tech › basics-of-javascript-string-substring-method-1083c3d918e2
Basics of Javascript · String · substring() (method) | by Jakub Korch | Medium
June 22, 2021 - This method extracts the characters from a string, between two specified indices, and returns the new substring.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › indexOf
String.prototype.indexOf() - JavaScript | MDN
The indexOf() method of String values searches this string and returns the index of the first occurrence of the specified substring. It takes an optional starting position and returns the first occurrence of the specified substring at an index greater than or equal to the specified number.
🌐
Scaler
scaler.com › home › topics › javascript string substring() method
JavaScript String substring() Method - Scaler Topics
October 7, 2022 - The substring() method is an inbuilt function in JavaScript that is used to extract a part of the string. Two parameters are passed to the function, i.e., the starting index and the ending index that tells the function which part of the string to grab.
🌐
Bennadel
bennadel.com › blog › 2159-using-slice-substring-and-substr-in-javascript.htm
Using Slice(), Substring(), And Substr() In Javascript
April 21, 2020 - In all cases, the second argument is optional. If it is not provided, the substring will consist of the start index all the way through the end of the string.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › difference-between-substr-and-substring-in-javascript
Difference between substr() and substring() in JavaScript - GeeksforGeeks
July 11, 2025 - The main difference is that substr() accepts a length parameter, while substring() accepts start and end indices.