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

Array.shift() algorithm description incorrect #38166

Closed
simonkcleung opened this issue Feb 15, 2025 · 1 comment · Fixed by #38171
Closed

Array.shift() algorithm description incorrect #38166

simonkcleung opened this issue Feb 15, 2025 · 1 comment · Fixed by #38171
Labels
Content:JS JavaScript docs

Comments

@simonkcleung
Copy link

MDN URL

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

What specific section or headline is this issue about?

No response

What information was incorrect, unhelpful, or incomplete?

In Description
The shift() method removes the element at the zeroth index and shifts the values at consecutive indexes down, ....

What did you expect to see?

It should be shifts the values at consecutive indexes down first and remove the last element...

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

Demo:
var array1 = [1, 2, 3]; Object.seal(array1); try {array1.shift()} catch(e){}; console.log(array1); // Output: Array [2, 3, 3]

Do you have anything more you want to share?

No response

MDN metadata

Page report details
@simonkcleung simonkcleung added the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label Feb 15, 2025
@github-actions github-actions bot added the Content:JS JavaScript docs label Feb 15, 2025
@Josh-Cena Josh-Cena changed the title Array.shift() Array.shift() algorithm description incorrect Feb 15, 2025
@Josh-Cena Josh-Cena removed the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label Feb 15, 2025
@Josh-Cena
Copy link
Member

I just tried to see how it would look, and I think the current description is within the acceptable range of correctness. The array method docs always start by describing what the user expects to happen—it paints a mental picture, not intended as an exact description of the algorithm. Normally when people think of removing at the beginning, they think of remove and then shift. If we describe it the first time in the most precise way, it's only going to confuse readers because "delete the last element" seems like an unintuitive way to do it. If you want to know the exact algorithm, it's mentioned in the Calling shift() on non-array objects section, because that's where you can actually observe a lot of the peculiarities of the method.

The shift() method reads the length property of this. If the normalized length is 0, length is set to 0 again (whereas it may be negative or undefined before). Otherwise, the property at 0 is returned, and the rest of the properties are shifted left by one. The length property is decremented by one.

However, I found that this paragraph does not mention deleting the last index, which we should.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:JS JavaScript docs
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants