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.
Discussions

string - javascript substring - Stack Overflow
the most darndest thing! the following code prints out 'llo' instead of the expected 'wo'. i get such surprising results for a few other numbers. what am i missing here? alert('helloworld'.substri... More on stackoverflow.com
🌐 stackoverflow.com
JavaScript Substring Vs Substr Vs Slice Differences
You'd think this warning on MDN would be enough to cover the part about substr: Warning: Although String.prototype.substr(…) is not strictly deprecated (as in "removed from the Web standards"), it is defined in Annex B of the ECMA-262 standard, whose introduction states: … All of the language features and behaviours specified in this annex have one or more undesirable characteristics and in the absence of legacy usage would be removed from this specification. … … Programmers should not use or assume the existence of these features and behaviours when writing new ECMAScript code. … For that matter, you'd think that the general documentation on MDN would be enough to cover the rest of it. More on reddit.com
🌐 r/javascript
1
3
August 31, 2018
Help-I can't figure out how to take a substring from a string ...
🌐 r/javascript
Exploring V8's strings: implementation and optimizations
Amazing article. The biggest, most practical, thing I learned was about Sliced Strings and how they hold a reference in memory to the full original string. That's definitely something I wouldn't expect and I can totally see how that would create a ton of extra memory usage for no good reason. Do you know which string methods create these string pointers? Is it anything that returns a substring? Eg: .match, .matchAll, .slice, .substr, .substring? More on reddit.com
🌐 r/javascript
20
40
November 14, 2023
🌐
Great Learning
mygreatlearning.com › blog › it/software development › javascript substring() method: variants and uses
JavaScript substring() Method: Variants and Uses
March 31, 2025 - The substring() method in JavaScript extracts parts of a string between specified indices. This article explores its syntax, usage, key differences from similar methods like slice(), and practical applications such as extracting usernames or ...
🌐
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.
🌐
Reddit
reddit.com › r/javascript › javascript substring vs substr vs slice differences
r/javascript on Reddit: JavaScript Substring Vs Substr Vs Slice Differences
August 31, 2018 - r/javascript • · kedar5 · arungudelli.com Open Share · Share · Reashu • 7y ago · You'd think this warning on MDN would be enough to cover the part about substr: Warning: Although String.prototype.substr(…) is not strictly deprecated (as in "removed from the Web standards"), it is defined in Annex B of the ECMA-262 standard, whose introduction states: …
Find elsewhere
🌐
BitDegree
bitdegree.org › learn › javascript-substring
Master JavaScript substring: Easily Add substring JavaScript
August 10, 2017 - The JavaScript string.substring() method extracts the symbols between two indexes and returns a data string.
🌐
Refine
refine.dev › home › blog › tutorials › javascript substring method
JavaScript Substring Method | Refine
December 31, 2024 - JavaScript substring() is a String method that is typically used to extract and store part of a string.
🌐
W3Schools
www-db.deis.unibo.it › courses › TW › DOCS › w3schools › jsref › jsref_substring.asp.html
JavaScript String substring() Method
The substring() method extracts the characters from a string, between two specified indices, and returns the new sub string.
🌐
DigitalOcean
digitalocean.com › community › tutorials › js-substring-vs-substr
substring vs substr in JavaScript | DigitalOcean
November 6, 2017 - const myStr = 'Alligator'; const myNewStrViaSubstring = myStr.substring(3); const myNewStrViaSubstr = myStr.substr(3); console.log(myNewStrViaSubstring); // igator console.log(myNewStrViaSubstr); // igator · Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases. ... Alligator.io is a developer-focused resource that offers tutorials and insights on a wide range of modern front-end technologies, including Angular 2+, Vue.js, React, TypeScript, Ionic, and JavaScript.
🌐
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!";
🌐
LearnersBucket
learnersbucket.com › home › examples › javascript › how to find the substring in javascript
How to find the substring in javascript - LearnersBucket
April 8, 2019 - If the startIndex is greater than or equal to the strings length then an empty substring will be returned.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › difference-between-string-slice-and-string-substring-in-javascript
Difference between String.slice and String.substring in JavaScript - GeeksforGeeks
July 11, 2025 - Example 2:In this Example, In the case of substring() it swaps the arguments when start>stop where the slice() returns the empty string.
🌐
Playwright
playwright.dev › writing tests
Writing tests | Playwright
import { test, expect } from '@playwright/test'; test('has title', async ({ page }) => { await page.goto('https://playwright.dev/'); // Expect a title "to contain" a substring. await expect(page).toHaveTitle(/Playwright/); }); test('get started link', async ({ page }) => { await page.goto('https://playwright.dev/'); // Click the get started link.
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › String › substring
JavaScript String substring() - Extract Substring Range | Vultr Docs
September 27, 2024 - The substring() method in JavaScript is essential for extracting parts of a string based on specified index positions.
🌐
W3Schools
w3schools.com › js › js_string_methods.asp
JavaScript String Methods
1 week ago - The substr() method is removed (deprecated) in the latest JavaScript standard.
🌐
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