Skip to content

Extend documentation of String.indexOf to mention what happens when substring is not foundΒ #60570

@Pascal-So

Description

@Pascal-So

πŸ” 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:

Image

But for strings, this information was missing thus far:

Image

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

  1. What do you want to use this for?

To get more useful information from the language server while writing TS, which reduces interruptions / context switching, allowing the user to be more productive.

  1. What shortcomings exist with current approaches?

The user has to stop and think about the not found case. Users coming from other languages might expect the indexOf function to maybe return null or undefined or throw an exception when the substring is not found.

  1. What workarounds are you using in the meantime?

In cases where I've personally been faced with this issue in the past, I quickly used a js console to check the behaviour.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions