Explore tens of thousands of sets crafted by our community.
Array Methods and Properties
10
Flashcards
0/10
push()
Adds one or more elements to the end of an array and returns the new length of the array. Complexity: O(1) amortized.
pop()
Removes the last element from an array and returns that element. Complexity: O(1).
shift()
Removes the first element from an array and returns that removed element. Complexity: O(n) as it may require shifting all other elements.
unshift()
Adds one or more elements to the beginning of an array and returns the new length. Complexity: O(n) as it may require shifting all other elements.
slice()
Returns a shallow copy of a portion of an array into a new array object. Complexity: O(n), where n is the number of elements in the selected range.
splice()
Changes the contents of an array by removing or replacing existing elements and/or adding new elements in place. Complexity: O(n), due to the need to shift elements.
concat()
Used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array. Complexity: O(n), where n is the total number of elements in the combined arrays.
indexOf()
Returns the first index at which a given element can be found in the array, or -1 if it is not present. Complexity: O(n), where n is the array length.
sort()
Sorts the elements of an array in place and returns the array. The default sort order is according to string Unicode code points. Complexity: O(n log n), on average, depending on the implementation.
length
The length property of an array represents the number of elements in the array. Complexity: O(1), as it is just accessing a property.
© Hypatia.Tech. 2024 All rights reserved.