javascript-que-son-arrays

What are and how to use Arrays in JavaScript

  • 5 min

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, ...];

Copied!

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"];
Copied!

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"
Copied!

If you try to access an index that doesn’t exist, you will get undefined:

console.log(frutas[10]); // undefined
Copied!

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]
Copied!

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"]
Copied!

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"]
Copied!

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]
Copied!

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]
Copied!

Searching for Elements

  • indexOf: Returns the index of the first element that matches the given value, or -1 if not found.
  • includes: Returns true if the array contains the given value.
const frutas = ["manzana", "naranja", "plátano"];
console.log(frutas.indexOf("naranja")); // 1
console.log(frutas.includes("pera")); // false
Copied!

Array Length

The .length property returns the number of elements in an array:

const miArray = [10, 20, 30];
console.log(miArray.length); // 3
Copied!

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
Copied!

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]);
}
Copied!

for…of loop

let numeros = [1, 2, 3, 4, 5];
for (let numero of numeros) {
    console.log(numero);
}
Copied!

forEach() loop

let numeros = [1, 2, 3, 4, 5];
numeros.forEach(function(numero) {
    console.log(numero);
});
Copied!

Practical Examples