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.
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.
Videos
00:57
substring() in Javascript 🔥 #javascript #DSA #javascriptinterview ...
06:08
How to use Substrings in Javascript - YouTube
07:04
JavaScript Substring Method in 5 Minutes - YouTube
06:15
slice vs substring method | String Object In JavaScript - YouTube
09:26
#76 The substring() & substr() Method | Array & String Methods ...
06:32
Master Substring Searches in JavaScript! Quick & Easy Tutorial” ...
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'
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.
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.
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.
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
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.
Facebook
facebook.com › groups › javascript.react.node › posts › 1784653785232316
What do subString() method in JavaScript do
We cannot provide a description for this page right now
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.