Strings In Javascript, and the practical examples still hold up.
That kirupa.com article is a classic. The .substring() method always gets me though. I always forget if it’s (start, end) or (start, length) and end up trying both.
const myString = "hello world";
// this gives "hello"
myString.substring(0, 5);
// this also gives "hello" but is technically different
myString.slice(0, 5);
It’s (start, end) for both, but slice can handle negative indexes which is kinda neat.
You nailed the part that trips everyone up — yes, it’s (start, end) for both, not (start, length). That’s the one I always have to double-check too.
Your slice point is the useful bit, though. The extra wrinkle worth remembering: substring quietly swaps the arguments and clamps negatives to 0, while slice doesn’t. So substring(5, 0) still gives you “hello”, but slice(5, 0) gives you nothing. That’s why slice(-5) is such a handy shortcut for “last five characters” and substring just can’t do that.
Nice catch on both.