What is an Array? Basic Array Operations in JavaScript

Tiempo de lectura: < 1 minuto

Reading Time: < 1 minute

Good morning, friends!

In today’s tutorial, I’m going to talk to you about Arrays in JavaScript.

First of all, what is an array?

An array in JavaScript is an object that allows you to store a collection of values of different types, including other objects and functions. Arrays are very useful for representing data sets in a more readable way and for iterating over them using loops or iteration functions.

Here are some examples of how you can create and use arrays in JavaScript:

  • Create an empty array:
let array = []; 
  • Create an array with elements:
let array = [1, 2, 3, 4, 5];
  • Iterate over an array:
let array = [1, 2, 3, 4, 5];

for (let i = 0; i < array.length; i++) {
  console.log(array[i]);
}

// Output: 1, 2, 3, 4, 5
  • Add elements to an array:
let array = [1, 2, 3, 4, 5];

array.push(6);

console.log(array);

// Output: [1, 2, 3, 4, 5, 6]
  • Remove elements from an array:
let array = [1, 2, 3, 4, 5];

array.splice(2, 1); // Removes the element at position 2 and a total of 1 element

console.log(array);

// Output: [1, 2, 4, 5]

I hope you like it. See you in the next one.

Leave a Comment