Extend documentation of String.indexOf to mention what happens when substring is not found #60570
Open
6 tasks done
Labels
Milestone
π Search Terms
function documentation
jsdoc
NOT-FOUND
β Viability Checklist
β Suggestion
The documentation of
String.indexOf
should mention that -1 is returned when the substring is not found.π Motivating Example
The documentation for indexOf on arrays already mentioned that the method returns -1 when the value is not found:
But for strings, this information was missing thus far:
Of course the user would have been able to puzzle this together by seeing that the return type is just number, not something like number | undefined, thus allowing them to figure out that the sentinel value probably must be -1. Nevertheless, having to stop and think about this is unnecessary friction for people who don't remember this fact by heart, for example people who jump between programming languages a lot.
π» Use Cases
To get more useful information from the language server while writing TS, which reduces interruptions / context switching, allowing the user to be more productive.
The user has to stop and think about the not found case. Users coming from other languages might expect the
indexOf
function to maybe returnnull
orundefined
or throw an exception when the substring is not found.In cases where I've personally been faced with this issue in the past, I quickly used a js console to check the behaviour.
The text was updated successfully, but these errors were encountered: