Skip to content

stringSliceMethods

Reports usage of substring instead of slice for string operations.

✅ This rule is included in the ts logical presets.

JavaScript provides three methods for extracting substrings: slice(), substr(), and substring(). This rule encourages consistent use of slice() because:

  • substr() is deprecated and removed from the ECMAScript specification
  • substring() has confusing behavior: it auto-swaps arguments when start > end, and treats negative indices as 0
  • slice() has consistent, predictable behavior that matches Array.prototype.slice()
const result = text.substr(1, 5);
const result = text.substring(1, 5);
const last = text.substring(text.length - 3);

This rule is not configurable.

If you’re maintaining legacy code that heavily uses substr() or substring() and refactoring would be too costly, you may need to disable this rule. Note that substr() is deprecated and may be removed from JavaScript engines in the future.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.