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()
AEconsB0resuAE <= sp ansty="le--0:#D6DEEB;--1:#403F">53teAB0subs5<3(sp ansty="le--0:#F78C6C;--1:#AA09">8<21sp ansty="le--0:#C792EA;--1:#8844">AE<, sp ansty="le--0:#F78C6C;--1:#AA09">8<25sp ansty="le--0:#D6DEEB;--1:#403F">53<);d ivclassNa="meco"><);d>
44AEco61B0re44A3F534<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B0s30<9821 spans="tyle--0:#C792EA;--1:#88">440<9826 spans="tyle--0:#D6DEEB;--1:#40">3F<53) ;divclass="Name">< 6)>
AEconsB0resuAE <= sp ansty="le--0:#D6DEEB;--1:#403F">53teAB0substri5<3(sp ansty="le--0:#F78C6C;--1:#AA09">8<21sp ansty="le--0:#C792EA;--1:#8844">AE<, sp ansty="le--0:#F78C6C;--1:#AA09">8<25sp ansty="le--0:#D6DEEB;--1:#403F">53<);d ivclassNa="meco"><);d>
44AEco61B0re44A3F534<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B0s30<9821 spans="tyle--0:#C792EA;--1:#88">440<9825 spans="tyle--0:#D6DEEB;--1:#40">3F<53) ;divclass="Name">< 5)>
AEconsB0laAE <= sp ansty="le--0:#D6DEEB;--1:#403F">53teAB0substri53(teA72lengAE <- sp ansty="le--0:#F78C6C;--1:#AA09">8<23sp ansty="le--0:#D6DEEB;--1:#403F">53<);d ivclassNa="meco"><);d>
44AEco61B044A3F534<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B0s34<4AE- spans="tyle--0:#F78C6C;--1:#AA">0<9823 spans="tyle--0:#D6DEEB;--1:#40">3F<53) ;divclass="Name"><-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.