Chapter 6

Arrays

Arrays are a fundamental part of programming. An array is a list of data. We can store a lot of data in one variable, which makes our code more readable and easier to understand. It also makes it much easier to perform functions on related data.

The data in arrays are called elements.

Here is a simple array:

// 1, 1, 2, 3, 5, and 8 are the elements in this array
let numbers = [1, 1, 2, 3, 5, 8];

Arrays can be created easily using array literals or with a new keyword.

const cars = ["Saab", "Volvo", "BMW"]; // using array literals
const cars = new Array("Saab", "Volvo", "BMW"); // using the new keyword

An index number is used to access the values of an array. The index of the first element in an array is always 0 as array indexes start with 0. The index number can also be used to change the elements of an array.

const cars = ["Saab", "Volvo", "BMW"];
console.log(cars[0]); 
// Result: Saab

cars[0] = "Opel"; // changing the first element of an array
console.log(cars);
// Result: ['Opel', 'Volvo', 'BMW']

Arrays are a special type of object. One can have objects in an array.

The length property of an array returns the count of numbers elements. Methods supported by Arrays are shown below:

Name Description
concat() Returns two or more combined arrays
join() Joins all elements in an array into a string
push() Adds one or more elements at the end of the array and returns the length
pop() Removes the last element of an array and returns that element
shift() Removes the first element of an array and returns that element
unshift() Adds one or more elements at the front of an array and returns the length
slice() Extracts the section of an array and returns the new array
at() Returns element at the specified index or undefined
splice() Removes elements from an array and (optionally) replaces them, and returns the array
reverse() Transposes the elements of an array and returns a reference to an array
flat() Returns a new array with all sub-array elements concatenated into it recursively up to the specified depth
sort() Sorts the elements of an array in place, and returns a reference to the array
indexOf() Returns the index of the first match of the search element
lastIndexOf() Returns the index of the last match of the search element
forEach() Executes a callback in each element of an array and returns undefined
map() Returns a new array with a return value from executing callback on every array item.
flatMap() Runs map() followed by flat() of depth 1
filter() Returns a new array containing the items for which callback returned true
find() Returns the first item for which callback returned true
findLast() Returns the last item for which callback returned true
findIndex() Returns the index of the first item for which callback returned true
findLastIndex() Returns the index of the last item for which callback returned true
every() Returns true if callback returns true for every item in the array
some() Returns true if callback returns true for at least one item in the array
reduce() Uses callback(accumulator, currentValue, currentIndex, array) for reducing purpose and returns the final value returned by callback function
reduceRight() Works similarly lie reduce() but starts with the last element

results matching ""

    No results matching ""