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()
@param ― start The index to the beginning of the specified portion of stringObj.
@param ― end The index to the end of the specified portion of stringObj. The substring includes the characters up to, but not including, the character indicated by end.
If this value is not specified, the substring continues to the end of stringObj.
Returns the substring at the specified location within a String object.
@param ― start The zero-based index number indicating the beginning of the substring.
@param ― end Zero-based index number indicating the end of the substring. The substring includes the characters up to, but not including, the character indicated by end.
If end is omitted, the characters from start through the end of the original string are returned.
@param ― start The index to the beginning of the specified portion of stringObj.
@param ― end The index to the end of the specified portion of stringObj. The substring includes the characters up to, but not including, the character indicated by end.
If this value is not specified, the substring continues to the end of stringObj.
Returns the substring at the specified location within a String object.
@param ― start The zero-based index number indicating the beginning of the substring.
@param ― end Zero-based index number indicating the end of the substring. The substring includes the characters up to, but not including, the character indicated by end.
If end is omitted, the characters from start through the end of the original string are returned.
@param ― start The index to the beginning of the specified portion of stringObj.
@param ― end The index to the end of the specified portion of stringObj. The substring includes the characters up to, but not including, the character indicated by end.
If this value is not specified, the substring continues to the end of stringObj.
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.