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
🌐
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 › 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.
🌐
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.
🌐
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.
🌐
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.
🌐
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!";
Find elsewhere
🌐
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
🌐
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.
🌐
Bennadel
bennadel.com › blog › 2159-using-slice-substring-and-substr-in-javascript.htm
Using Slice(), Substring(), And Substr() In Javascript
April 21, 2020 - 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.
🌐
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.
🌐
Socrata
dev.socrata.com › docs › datatypes › text.html
Text Datatype | Socrata
javascript · PHP · PowerShell · Python (Dataset Management API) Python · R · Ruby · Examples · Visualizing data using the Google Calendar Chart · Scrubbing data with Python · Gauge Visualizations using the Google Charts library · Pulling data from Hadoop and Publishing to Socrata ·
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › split
String.prototype.split() - JavaScript | MDN
const splitByNumber = { [Symbol.split](str) { let num = 1; let pos = 0; const result = []; while (pos < str.length) { const matchPos = str.indexOf(num, pos); if (matchPos === -1) { result.push(str.substring(pos)); break; } result.push(str.substring(pos, matchPos)); pos = matchPos + String(num).length; num++; } return result; }, }; const myString = "a1bc2c5d3e4f"; console.log(myString.split(splitByNumber)); // [ "a", "bc", "c5d", "e", "f" ]
🌐
Codecademy
codecademy.com › docs › javascript › substring
JavaScript | Substring | Codecademy
May 29, 2025 - The .substring() method in JavaScript extracts a portion of a string from one position to another (exclusive) and returns a 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.