Skip to content

Commit

Permalink
Improve Array.p.shift() algorithm description (#38171)
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena authored Feb 16, 2025
1 parent 5907e15 commit 2e6d901
Showing 1 changed file with 2 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ The removed element from the array; {{jsxref("undefined")}} if the array is empt

## Description

The `shift()` method removes the element at the zeroth index and shifts the
values at consecutive indexes down, then returns the removed value. If the
{{jsxref("Array/length", "length")}} property is 0, {{jsxref("undefined")}} is returned.
The `shift()` method shifts all values to the left by 1 and decrements the length by 1, resulting in the first element being removed. If the {{jsxref("Array/length", "length")}} property is 0, {{jsxref("undefined")}} is returned.

The {{jsxref("Array/pop", "pop()")}} method has similar behavior to `shift()`, but applied to the last element in an array.

Expand Down Expand Up @@ -89,7 +87,7 @@ while (typeof (i = names.shift()) !== "undefined") {

### Calling shift() on non-array objects

The `shift()` method reads the `length` property of `this`. If the [normalized length](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#normalization_of_the_length_property) 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.
The `shift()` method reads the `length` property of `this`. If the [normalized length](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#normalization_of_the_length_property) 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 property at `length - 1` is [deleted](/en-US/docs/Web/JavaScript/Reference/Operators/delete), and the `length` property is decremented by one.

```js
const arrayLike = {
Expand Down

0 comments on commit 2e6d901

Please sign in to comment.