Sprints: Incorrect string.indexOf description

Created on 11 Nov 2019  Â·  8Comments  Â·  Source: mdn/sprints

Request type

  • [ ] Correction or update

Details


The description of the fromIndex parameter says that if the parameter is >= than str.length the method returns -1, but that is false.

JS Small P1

Most helpful comment

@Leokuma cool, I'm glad it is helpful!

For the pt-BR, I'm sure you write much better Portuguese than any of us ;-) We have no Portuguese speakers on the team.

All 8 comments

That is actually correct.

Having a fromIndex equal or greater than length means that the search will start after the last character of the string, which doesn't make sense. Remember that fromIndex starts at 0.

image

Oh, sorry, i initially thought it worked like that, but then i got confused because of this one example where it returned 10 even though fromIndexwas > than length, and after some testing i now understand that it didn't return -1 because the string it was looking for is an empty string, and in that case apparently it just returns the length of the string. Thanks for the answer!
image

Hum, you do have a point, though:

image
So, when searching for an empty string, it behaves somewhat unpredictably.

Yeah, i guess there is some sort of an error after all! Well, thanks again for the answers!

This is an interesting case — I do agree that the behavior and wording here is a little confusing.

I've done some testing and added some wording to

https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf#Syntax

Does this help?

For me it's perfect.

I can try to update the Portuguese version myself, or I can leave that with you specialists :+1:.

@Leokuma cool, I'm glad it is helpful!

For the pt-BR, I'm sure you write much better Portuguese than any of us ;-) We have no Portuguese speakers on the team.

I updated the pt-BR page.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

acolle picture acolle  Â·  4Comments

Elchi3 picture Elchi3  Â·  4Comments

marcvangend picture marcvangend  Â·  5Comments

wbamberg picture wbamberg  Â·  6Comments

krankj picture krankj  Â·  3Comments