Arrays

Arrays

Arrays are a fundamental data structure in JavaScript that allow you to store and manage collections of values. They provide a convenient way to work with lists of items. In this section, we'll explore how to create, manipulate, and work with arrays in JavaScript.

1. What is an Array?

An array is an ordered collection of values, known as elements. Each element in an array is associated with an index, starting at 0 for the first element. You can store various data types in an array, including numbers, strings, objects, or even other arrays.

2. Creating Arrays

There are several ways to create arrays in JavaScript:

  • Using Array Literals: The simplest way to create an array is by using square brackets [] and placing the elements inside.

    let fruits = ['apple', 'banana', 'cherry'];
    
  • Using the Array Constructor: You can also create an array using the Array constructor.

    let colors = new Array('red', 'green', 'blue');
    

3. Accessing Array Elements

You can access elements in an array using their index. The index is a zero-based integer, where 0 corresponds to the first element, 1 to the second element, and so on.

let fruits = ['apple', 'banana', 'cherry'];
let firstFruit = fruits[0]; // 'apple'
let secondFruit = fruits[1]; // 'banana'

4. Modifying Array Elements

You can change the value of an array element by assigning a new value to it using its index.

fruits[1] = 'kiwi';
console.log(fruits); // ['apple', 'kiwi', 'cherry']

5. Array Properties and Methods

JavaScript arrays have several useful properties and methods, such as:

  • length: Returns the number of elements in the array.

  • push(): Adds one or more elements to the end of the array.

  • pop(): Removes the last element from the array and returns it.

  • unshift(): Adds one or more elements to the beginning of the array.

  • shift(): Removes the first element from the array and returns it.

  • concat(): Combines two or more arrays and returns a new array.

  • join(): Joins all elements into a single string with a specified separator.

  • slice(): Returns a shallow copy of a portion of the array.

  • splice(): Adds or removes elements from the array.

  • indexOf(): Returns the first index at which a specified element is found in the array.

  • includes(): Checks if an element is included in the array.

  • filter(): Creates a new array with elements that pass a test.

  • map(): Creates a new array with the results of applying a function to each element.

  • reduce(): Reduces an array to a single value by applying a function to each element.

  • forEach(): Executes a provided function once for each array element.

  • sort(): Sorts the elements of the array in place and returns the sorted array.

  • reverse(): Reverses the order of the elements in the array.

6. Multi-Dimensional Arrays

Arrays can also hold other arrays, creating multi-dimensional arrays. For example, a two-dimensional array can be used to represent a grid of values.

let matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];

7. Iterating Through Arrays

You can use loops like for, while, or forEach to iterate through the elements of an array and perform operations on them.

let numbers = [1, 2, 3, 4, 5];

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

numbers.forEach(function (number) {
  console.log(number);
});

Understanding arrays is fundamental in JavaScript as they are commonly used to store, manipulate, and retrieve data efficiently. You'll frequently encounter arrays when working on JavaScript applications.

Next Objects