Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing Doc: Array.prototype.join calls toString internally #27134

Closed
amustaque97 opened this issue Jun 4, 2023 · 3 comments · Fixed by #27345
Closed

Missing Doc: Array.prototype.join calls toString internally #27134

amustaque97 opened this issue Jun 4, 2023 · 3 comments · Fixed by #27345
Labels
Content:JS JavaScript docs help wanted If you know something about this topic, we would love your help!

Comments

@amustaque97
Copy link

MDN URL

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join

What specific section or headline is this issue about?

Examples

What information was incorrect, unhelpful, or incomplete?

We should demonstrate an example where we tell the readers that if there are nested arrays as items of an array variable If we call the join method on the variable it will internally call toString on the nested array item with , as a delimiter. This will return a flattering array ( which may confuse the users if their delimiter is also comma , ), for example below

Code snippet

const x = ['h', 'e', 'l', 'l', 'o'];

console.log([[[[[[[[x]]]]]]], 'w', 'o', 'r', 'l', 'd'].join(','))

output:

"h,e,l,l,o,w,o,r,l,d" 

What did you expect to see?

Examples like this can be really helpful if someone is a beginner and it can help people during dev or code reviews.

Do you have any supporting links, references, or citations?

Under the spec:
https://tc39.es/ecma262/multipage/indexed-collections.html#sec-array.prototype.join

Read point 7.b

Do you have anything more you want to share?

If we all agree on this, I'm happy to raise an MR.

@amustaque97 amustaque97 added the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label Jun 4, 2023
@github-actions github-actions bot added the Content:JS JavaScript docs label Jun 4, 2023
@Josh-Cena
Copy link
Member

I'm not sure what you could add. The fact that "Array.prototype.join calls toString internally" is mentioned on both https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toString. What behavior should it have otherwise, anyway?

@amustaque97
Copy link
Author

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toString

I agree toString page mentions calling the join method but I'm not able to find that line in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join join says it calls toString internally though it only says "new string by concatenating all of the elements in an array" but how it happens we don't tell the reader.

Thoughts?

@Josh-Cena
Copy link
Member

Ummm... I see your point. join and toString are mutually recursive. Sure, we can add a sentence or a few examples.

Fun fact: when there's a mutually recursive call you naturally think about what happens when there is a loop:

const a = [];
a[0] = a;
a.join();

And, you will find that none of the browsers get stuck! See tc39/ecma262#289. Before the issue is closed, I don't think we have to mention this edge case; just some food for thought.

@Josh-Cena Josh-Cena added help wanted If you know something about this topic, we would love your help! and removed needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. labels Jun 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:JS JavaScript docs help wanted If you know something about this topic, we would love your help!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants