array.push()
passing zero or one argument
GIVEN an empty array WHEN pushing nothing to it THEN the array stays empty
GIVEN an array with one item WHEN pushing nothing to it THEN the call returns 1
, the length of the array
GIVEN an empty array WHEN calling arr.push("a")
THEN the array just contains "a"
GIVEN an array with 3 items WHEN calling push(1)
into it THEN this call return the new length, 4
passing many arguments
GIVEN an empty array WHEN calling push(1, 2)
THEN the array contains [1, 2]
GIVEN an array with 2 items WHEN calling push(1, 2)
THEN the array contains [1, 2, 1, 2]
calling push on an non-array
WHEN calling push(42)
on an array-like object (one that has the property length=0
) THEN a property is added at index "0" AND length
is increased
WHEN calling push(42)
on an object with length=3
THEN the 42
is assigned to the property "3"
AND the length is increased
WHEN calling push()
on a string (which also has length
) THEN it throws, since length
is read-only
WHEN calling push()
on a function (which also has length
) THEN it throws, since length
is read-only
WHEN calling push()
on a Date.UTC
(where length
is 7) THEN it throws, since length
is read-only
Links
Very well readable, easy to understand description of how push() works.
The version of the specification where `array.push()` was introduced, see section 15.4.4.7. (PDF 723kB)
Related Katas
Array API
array.sort()
basicsarray.sort()
can take a compare functionarray.shift()
(as introduced in ES3)array.push()
(as introduced in ES3)Array.from()
Array.of()
array.fill()
array.find()
array.findIndex()
array.entries()
array.keys()
array.values()
array.includes()
array.toReversed()
Difficulty Level
INTERMEDIATE
First Published
28 March 2024
Stats
11 tests to solve