JS Arrays 🎯

beginner
25 min

JS Arrays 🎯

Welcome to our deep dive into JavaScript (JS) Arrays! In this lesson, we'll learn everything you need to know about arrays, from the basics to advanced examples. 📝

What are Arrays?

Arrays are a special type of variable in JavaScript that can hold multiple values. They allow you to store and manipulate lists, collections, and sequences of data. 💡

Here's a simple example of an array:

javascript
// Declare an array with the name 'myArray' const myArray = [1, 2, 3, 4, 5];

In the above example, we've created an array called myArray containing five numbers. Each number is referred to as an element within the array.

Accessing Array Elements 📝

To access an element in an array, you use its index number. In JavaScript, array indices start at 0. Let's access the first and last elements of our myArray:

javascript
// Access the first element console.log(myArray[0]); // Output: 1 // Access the last element console.log(myArray[myArray.length - 1]); // Output: 5

Changing Array Elements 📝

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

javascript
// Change the first element to 'apple' myArray[0] = 'apple'; console.log(myArray); // Output: ['apple', 2, 3, 4, 5]

Array Length 📝

The length property of an array returns the number of elements it contains:

javascript
console.log(myArray.length); // Output: 5

Adding Elements to an Array 📝

You can add new elements to an array using the push() method:

javascript
// Add 'banana' to the end of the array myArray.push('banana'); console.log(myArray); // Output: ['apple', 2, 3, 4, 5, 'banana']

Removing Elements from an Array 📝

To remove an element from an array, you can use the splice() method. Here's how to remove the first element:

javascript
// Remove the first element myArray.splice(0, 1); console.log(myArray); // Output: [2, 3, 4, 5, 'banana']

Multidimensional Arrays 📝

Multidimensional arrays are arrays within arrays. They're useful for organizing complex data structures:

javascript
// Declare a multidimensional array const myArray2D = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; console.log(myArray2D[1][1]); // Output: 5

Arrays Methods 📝

JavaScript provides several useful methods for arrays. Here are a few examples:

  • indexOf(): Finds the index of a specific value in the array
  • includes(): Checks if the array includes a specific value
  • forEach(): Iterates through each element in the array
  • map(): Creates a new array with the results of calling a provided function on every element in the array
  • filter(): Creates a new array with all elements that pass the test provided by a function
  • reduce(): Reduces the array to a single value by repeatedly applying a function to each element

Quiz 📝

Quick Quiz
Question 1 of 1

What is the output of `console.log(myArray[0]);` in the first example?


This is just the beginning of our JS Arrays lesson. In the following sections, we'll dive deeper into array methods and explore real-world examples. Stay tuned! 🚀