JS Built-ins
← String138 / 195
StringString.prototype.substringintermediate

String.prototype.substring() β€” start > end swaps arguments

πŸ“– Usage Example
const str = 'Hello, World!'
str.substring(7, 12)   // β†’ 'World'
str.substring(5, 2)    // β†’ 'llo' (args swapped)
str.substring(7)       // β†’ 'World!'

Use substring() to extract characters between two indices, automatically swapping them if start is greater than end.

String.prototype.substring() β€” Argument Swap

Unlike slice, if start > end, .substring() swaps them automatically.

Challenge: Implement substringSwap(str) that calls substring(5, 2) and relies on auto-swap.

substringSwap('abcdefgh') // β†’ 'cde'  (same as substring(2, 5))
πŸ’‘ Hints (1)
  • β€’ `str.substring(a, b)` treats the arguments as `substring(min(a,b), max(a,b))`.