Skip to content

unicorn/prefer-string-slice Pedantic

🛠️ An auto-fix is available for this rule.

What it does

Prefer String#slice() over String#substr() and String#substring().

Why is this bad?

String#substr() and String#substring() are the two lesser known legacy ways to slice a string. It's better to use String#slice() as it's a more popular option with clearer behavior that has a consistent Array counterpart.

Example

Examples of incorrect code for this rule:

javascript
"foo".substr(1, 2);

Examples of correct code for this rule:

javascript
"foo".slice(1, 2);

References

Released under the MIT License.