Skip to content

Files

Latest commit

 

History

History
20 lines (14 loc) · 469 Bytes

prefer-string-slice.md

File metadata and controls

20 lines (14 loc) · 469 Bytes

Pattern: Use of substr() or substring()

Issue: -

Description

The slice() method provides a more consistent way to extract a portion of a string compared to legacy methods like substr() and substring(). It has clearer behavior and matches the behavior of Array.prototype.slice().

Examples

Example of incorrect code:

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

Example of correct code:

"foo".slice(1, 2);