Arrays

An array is an object which stores an ordered collection of values. This can hold values of any data type (numbers, strings, objects, functions, or other arrays), storing them in a single variable.

  • We declare an array as a variable, using the [] characters and inside the items separated with commas

let fruits = ["apple", "banana", "cherry"]; // Declaring an array
let diversity = ["apple", 2, {}, 4.21]; // An array with different elements
let numbers = new Array(1, 2, 3, 4); // Using the Array constructor
let empty = []; // An empty array

print(fruits) //The are printed entirely

  • The elements in an array can be accessed using indexes (positions), determined by integer numbers that start at 0. We use the name of the variable and the index value between the [] characters. Each element can also be stored in other variables.

let days = ["monday","tuesday","wednesday", "thursday"]
console.log(days[0])     // This prints "monday"

let second = days[2]; // Storing a element in a variable
console.log(second)     //  This prints "wednesday"

  • If we access a position that is outside the length of the array, we will get a undefined value. Also, if we assign a value to a position out of index, this will create empty spots in the array

let days = ["monday","tuesday","wednesday", "thursday"];
console.log(days[1]);    // This prints "tuesday"
console.log(days[9]);     // This prints undefined

let arr = [];
a[5] = 42;    // Assign value out of index
console.log(arr); // This prints [5 empty items, 42 ]

  • We can update any element of an array by assigning a new value to the objective index. This works even if the array was declared as a constant, but the entire array can't be reassigned


  • The length attribute is crucial for working with an array, indicating the number of elements that the array stores.


  • The push() method lets us add items to the end of the array or in a specific position. In the same way, the pop() method removes the item in the last position


  • There are a lot of other useful array methods. Some examples are:


  • Arrays can also be nested to contain other arrays, and in the same the indexes are nested to access elements inside arrays of arrays

Iterators

Iterators are the built-in JavaScript array methods that help us iterate over the elements of an array. They are methods called on arrays to manipulate elements and return values.

  • We can traverse all elements in an array with loops


  • Using .forEach() will execute the same code for each element of the array. This just use the elements and returns undefined when finished


  • Using .map() will create a new array by transforming each item with a function. It works like .forEach(), but has to be assigned to a variable to save the resultant array


  • Using .filter() returns an array of elements after filtering out certain elements based on the callback function that verifies a statement that returns true or false depending on the element. Those that return true, are added to the new array, and if there isn’t any element that satisfies the condition, the function will return an empty array


  • Using .findIndex() will return the index of the first element that evaluates to true in the callback function. If there isn’t any element that satisfies the condition, the function will return -1


  • Using .reduce() returns a single value after iterating through the elements of an array, in other words, accumulates a value based on every element of an array. In the callback function, the first parameter is an accumulator variable, and the second one is the iterator. It can also take an optional second parameter to set an initial value for the accumulator


  • Using .some() returns true if there is at least one element of an array that passes a condition, otherwise it returns false


  • Using .every() returns true if every element of an array passes a condition, otherwise it returns false

Last updated