🌐 Frontend & Web

JavaScript's substring(): Quirky, Useful, Utterly Confusing

JavaScript's substring() looks innocent. It bites with swapped args and NaN forgiveness—tripping even vets.

Diagram showing JavaScript string indices and substring extraction ranges

⚡ Key Takeaways

  • substring() auto-swaps args and forgives NaN/negatives—handy but hides bugs.
  • Prefer slice() for negatives and predictability; substring for legacy tolerance.
  • Always validate indices; JS strings butcher UTF-8 emojis without care.
Published by

DevTools Feed

Ship faster. Build smarter.

Worth sharing?

Get the best Developer Tools stories of the week in your inbox — no noise, no spam.

Originally reported by dev.to

Stay in the loop

The week's most important stories from DevTools Feed, delivered once a week.