JavaScript substring() Method: Complete Guide with Examples
JavaScript's substring() method extracts characters between two index positions in a string and returns the result as a new string. It is one of the most commonly used string methods in the languag...

Source: DEV Community
JavaScript's substring() method extracts characters between two index positions in a string and returns the result as a new string. It is one of the most commonly used string methods in the language, appearing in everything from URL parsing to input validation to display text truncation. Despite being straightforward in concept, substring() has several behavioral quirks -- particularly around negative indices and argument swapping -- that trip up even experienced developers. This guide covers the full substring() API with visual indexing examples, a detailed comparison with slice() and the deprecated substr(), TypeScript type patterns, real-world use cases, common mistakes, and performance considerations. What Is substring()? The substring() method extracts a portion of a string between a start index and an end index, returning the extracted part as a new string. The original string is never modified -- strings in JavaScript are immutable. Syntax string.substring(indexStart) string.sub