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.
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
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
Help-I can't figure out how to take a substring from a string ...
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
Videos
05:39
JavaScript Substring Example | Slice, Substr, and Substring Methods ...
03:06
How substring method works on a JavaScript String? - YouTube
07:04
JavaScript Substring Method in 5 Minutes - YouTube
06:32
Master Substring Searches in JavaScript! Quick & Easy Tutorial” ...
00:57
substring() in Javascript 🔥 #javascript #DSA #javascriptinterview ...
06:08
How to use Substrings in Javascript - YouTube
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'
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: …
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.
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.
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.
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
NeetCode
neetcode.io › problems › longest-substring-without-duplicates › solution
Longest Substring Without Repeating Characters
A better way to prepare for coding interviews.