I would like to propose the following rule:
Prefer slice()
over substring()
In ECMA String.prototype.slice
and String.prototype.substring
are defined to perform the same operation on a string
instance. The difference in behavior only exists when the arguments provided (start
and end
), are not provided as numbers, or when start
is bigger than end
, in which slice()
does not try to be forgiving, which is considered a good thing.
Non-compliant
function getCountry(iban: string): string {
return iban.substring(0, 2);
}
Compliant
function getCountry(iban: string): string {
return iban.slice(0, 2);
}
See: javascript - What is the difference between String.slice and String.substring? - Stack Overflow