Introduction to JavaScript Arrays

Tutorial 2 of 5

Introduction to JavaScript Arrays

1. Introduction

Goal of the Tutorial

This tutorial aims to introduce the basics of JavaScript arrays. You will learn how to create, access, and modify arrays, and understand their importance in JavaScript programming.

Learning Outcomes

By the end of this tutorial, you will be able to:
- Define and create JavaScript arrays
- Access and modify array elements
- Understand and use basic array methods

Prerequisites

Basic knowledge of JavaScript is required, including understanding of variables and data types.

2. Step-by-Step Guide

An array is a special variable that can hold multiple values at a time. It is useful when you want to store multiple values under a single name.

Creating an Array

In JavaScript, an array can be created in two ways:
1. Array Literal: This is the most common way to create an array.

let fruits = ["apple", "banana", "mango"];
  1. Array Constructor: Though it's less common, you can also create an array with the new keyword.
let fruits = new Array("apple", "banana", "mango");

Accessing Array Elements

Array elements are accessed using their index number. Array indices start from 0, meaning the first element is at index 0, the second at index 1, and so on.

let firstFruit = fruits[0]; // Outputs: apple

Modifying Array Elements

You can modify an existing element in an array by accessing it directly and assigning a new value.

fruits[1] = "blueberry"; // Changes the second element (banana) to blueberry

Useful Array Methods

JavaScript arrays have several methods that can be used to manipulate them. Some of the most common ones include:
- push(): Adds a new element to the end of an array
- pop(): Removes the last element from an array
- shift(): Removes the first element from an array
- unshift(): Adds a new element to the start of an array
- length: A property that returns the number of elements in an array

3. Code Examples

Creating, Accessing, and Modifying an Array

let fruits = ["apple", "banana", "mango"]; // Create an array
console.log(fruits[0]); // Access the first element - Outputs: apple

fruits[1] = "blueberry"; // Modify the second element
console.log(fruits); // Outputs: ["apple", "blueberry", "mango"]

Using Array Methods

let fruits = ["apple", "banana", "mango"]; // Create an array

fruits.push("orange"); // Add a new element to the end
console.log(fruits); // Outputs: ["apple", "banana", "mango", "orange"]

let lastFruit = fruits.pop(); // Remove the last element
console.log(lastFruit); // Outputs: orange
console.log(fruits.length); // Outputs: 3

4. Summary

In this tutorial, we have covered the basics of JavaScript arrays. We learned how to create, access, and modify arrays, and we discussed some basic array methods.

To further your understanding of JavaScript arrays, explore more complex array methods like sort(), splice(), slice(), and concat(). You can also learn about multidimensional arrays.

5. Practice Exercises

  1. Create an array of your favorite movies, then add a new movie to the end of the array and remove the first movie from the array.
  2. Write a function that accepts an array of numbers and returns the largest number in the array.
  3. Create a multidimensional array representing a matrix of numbers and write a function to transpose the matrix.

Solutions:

  1. Adding and Removing Movies
let movies = ["Inception", "The Dark Knight", "Memento"];
movies.push("Dunkirk");
movies.shift();
console.log(movies); // Outputs: ["The Dark Knight", "Memento", "Dunkirk"]
  1. Finding the Largest Number
function findMax(arr) {
    return Math.max(...arr);
}
console.log(findMax([1, 2, 3, 4, 5])); // Outputs: 5
  1. Transposing a Matrix
function transpose(matrix) {
    return matrix[0].map((_, i) => matrix.map(row => row[i]));
}
console.log(transpose([[1,2,3],[4,5,6],[7,8,9]])); // Outputs: [[1,4,7],[2,5,8],[3,6,9]]