Week 6: Mastering Arrays - Your First Collection
Learn how to organize, access, and manipulate collections of data using arrays in JavaScript.
Explore Chapter 6Common Array Methods: Modifying and Manipulating Arrays.
JavaScript arrays come with a rich set of built-in methods for adding, removing, and manipulating elements.
Adding/Removing Elements:
- push(element1, ..., elementN): Adds one or more elements to the end of the array and returns the new length. (Modifies original array).
let pets = ["dog", "cat"]; let newLength = pets.push("hamster", "goldfish"); console.log(pets); // ["dog", "cat", "hamster", "goldfish"] console.log(newLength); // 4 - pop(): Removes the last element from the array and returns that element. (Modifies original array).
let lastPet = pets.pop(); console.log(pets); // ["dog", "cat", "hamster"] console.log(lastPet); // "goldfish" - unshift(element1, ..., elementN): Adds one or more elements to the beginning of the array and returns the new length. (Modifies original array).
pets.unshift("rabbit"); console.log(pets); // ["rabbit", "dog", "cat", "hamster"] - shift(): Removes the first element from the array and returns that element. (Modifies original array).
let firstPet = pets.shift(); console.log(pets); // ["dog", "cat", "hamster"] console.log(firstPet); // "rabbit"
Slicing and Splicing:
- slice(startIndex, endIndex): Returns a new array containing a shallow copy of a portion of the original array from startIndex (inclusive) up to endIndex (exclusive). Does not modify the original array.
let numbers = [10, 20, 30, 40, 50, 60]; let middleNums = numbers.slice(2, 5); // Start at index 2, end before index 5 console.log(middleNums); // [30, 40, 50] console.log(numbers); // [10, 20, 30, 40, 50, 60] (original unchanged) let endNums = numbers.slice(3); // From index 3 to the end console.log(endNums); // [40, 50, 60] - splice(startIndex, deleteCount, item1, ..., itemN): Changes the contents of an array by removing or replacing existing elements and/or adding new elements in place. Returns an array containing the deleted elements. (Modifies original array).
let letters = ['a', 'b', 'c', 'd', 'e']; // Remove 2 elements starting from index 1 let removed = letters.splice(1, 2); console.log(letters); // ['a', 'd', 'e'] console.log(removed); // ['b', 'c'] // Remove 1 element at index 1, and insert 'X', 'Y' letters.splice(1, 1, 'X', 'Y'); console.log(letters); // ['a', 'X', 'Y', 'e'] // Insert 'Z' at index 2 without removing anything letters.splice(2, 0, 'Z'); console.log(letters); // ['a', 'X', 'Z', 'Y', 'e']
Other Useful Methods:
- indexOf(searchElement, fromIndex) / lastIndexOf(): Returns the first (or last) index at which a given element can be found, or -1 if not present.
- includes(searchElement, fromIndex): Returns true if an array includes a certain value, false otherwise.
- join(separator): Joins all elements of an array into a string, separated by the specified separator (default is comma).
- reverse(): Reverses the order of elements in the array in place.
- sort(): Sorts the elements of an array in place. (Default sort is often lexicographical/string-based, requires a compare function for numerical sort).