In Javascript, Arrays are a data structure that allows us to work with an ordered collection of elements, where each element has a numerical position (known as an index).
Array indices in JavaScript start from 0, meaning the first element has an index of 0, the second an index of 1, and so on.
In JavaScript, arrays are dynamic, meaning they can change in size and contain values of different types.
If you want to learn more, check out the Introduction to Programming Course
Creating Arrays
To declare an Array in Javascript, use the following syntax:
let miArray = [valor1, valor2, valor3, ...];
Where valor1, valor2, valor3, etc. are the values we want to store in the Array.
In Javascript, an Array can contain values of any data type (including other objects and functions).
let numeros = [1, 2, 3, 4, 5];
let frutas = ["manzana", "banana", "cereza"];
Accessing Elements
To access elements of an Array in Javascript, we use the numerical index corresponding to each element.
The first element of an Array has an index of 0, the second has an index of 1, and so on.
For example, if we want to access the second element of an Array named miArray, we can do it as follows:
let frutas = ["manzana", "banana", "cereza"];
console.log(frutas[0]); // "manzana"
console.log(frutas[1]); // "banana"
console.log(frutas[2]); // "cereza"
If you try to access an index that doesn’t exist, you will get undefined:
console.log(frutas[10]); // undefined
Modifying Elements
We can also modify the value of an Array element using its numerical index:
let numeros = [1, 2, 3, 4, 5];
numeros[2] = 10;
console.log(numeros); // [1, 2, 10, 4, 5]
Basic Usage
Adding Elements
push: Adds elements to the end of the array.unshift: Adds elements to the beginning of the array.
const frutas = ["manzana", "naranja"];
frutas.push("plátano");
console.log(frutas); // ["manzana", "naranja", "plátano"]
frutas.unshift("pera");
console.log(frutas); // ["pera", "manzana", "naranja", "plátano"]
Removing Elements
pop: Removes the last element from the array.shift: Removes the first element from the array.
const frutas = ["manzana", "naranja", "plátano"];
frutas.pop();
console.log(frutas); // ["manzana", "naranja"]
frutas.shift();
console.log(frutas); // ["naranja"]
Accessing a Fragment
slice: Returns a copy of a portion of the array without modifying the original.
const numeros = [1, 2, 3, 4, 5];
const subArray = numeros.slice(1, 4);
console.log(subArray); // [2, 3, 4]
Modifying Content
splice: Adds, removes, or replaces elements in an array.
const numeros = [1, 2, 3, 4, 5];
numeros.splice(2, 1, 99); // Replaces the element at index 2 with 99
console.log(numeros); // [1, 2, 99, 4, 5]
Searching for Elements
indexOf: Returns the index of the first element that matches the given value, or-1if not found.includes: Returnstrueif the array contains the given value.
const frutas = ["manzana", "naranja", "plátano"];
console.log(frutas.indexOf("naranja")); // 1
console.log(frutas.includes("pera")); // false
Array Length
The .length property returns the number of elements in an array:
const miArray = [10, 20, 30];
console.log(miArray.length); // 3
Multidimensional Arrays
Arrays in JavaScript can contain other arrays as elements, allowing the creation of multidimensional arrays.
let matriz = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
console.log(matriz[0][0]); // 1
console.log(matriz[1][2]); // 6
Iterating Over an Array
We can iterate over the elements of an Array in different ways
for loop
let numeros = [1, 2, 3, 4, 5];
for (let i = 0; i < numeros.length; i++) {
console.log(numeros[i]);
}
for…of loop
let numeros = [1, 2, 3, 4, 5];
for (let numero of numeros) {
console.log(numero);
}
forEach() loop
let numeros = [1, 2, 3, 4, 5];
numeros.forEach(function(numero) {
console.log(numero);
});
Practical Examples
Average of Numbers
Let’s calculate the average of a list of numbers:
const numeros = [10, 20, 30, 40, 50];
const suma = numeros.reduce((acum, num) => acum + num, 0);
const promedio = suma / numeros.length;
console.log(promedio); // 30
Transforming an Array with map
The .map() method creates a new array by applying a function to each element:
const numeros = [1, 2, 3];
const cuadrados = numeros.map((num) => num ** 2);
console.log(cuadrados); // [1, 4, 9]
Filtering Elements with filter
The .filter() method returns a new array with the elements that meet a condition:
const numeros = [1, 2, 3, 4, 5];
const pares = numeros.filter((num) => num % 2 === 0);
console.log(pares); // [2, 4]
Reducing an Array with reduce
The .reduce() method allows combining all elements of an array into a single value:
const numeros = [1, 2, 3, 4];
const suma = numeros.reduce((acumulador, actual) => acumulador + actual, 0);
console.log(suma); // 10
