Cityscape
Girl

JavaScript

Arrays - Storing Multiple Values

Easy
10 min

What Is an Array?

An array is a special kind of variable that lets you store a list of values together. Instead of a single wire that connects a name to one value, imagine an array as a long wire with many slots—each holding a single value. These slots are called indices, and they are numbered starting at 0.

For example, consider the following array:

let fruits = ["apple", "banana", "cherry"];

In this code we declare a variable fruits using let and assign it an array of three string values. Each value is stored at a specific index:

  • "apple" is at index 0
  • "banana" is at index 1
  • "cherry" is at index 2
Drawing canvas

Creating an Array

To create an array, you use square brackets with a comma-separated list of values:

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

You can also create an empty array and add values later:

let emptyArray = [];
emptyArray[0] = "first";
emptyArray[1] = "second";

Accessing Array Elements

You can access an element by using its index. For example, to get the first element of the fruits array, you write:

console.log(fruits[0]); // prints "apple"

The number inside the square brackets tells JavaScript which item you want to read.

fruits[1] = "blueberry";
console.log(fruits); // prints ["apple", "blueberry", "cherry"]

Updating Array Elements

You can change the value at any index. For example, if you want to change "banana" to "blueberry" in the fruits array:

fruits[1] = "blueberry";
console.log(fruits); // prints ["apple", "blueberry", "cherry"]

This “reconnects” the wire at index 1 to a new value.

Adding Elements with push()

The push() method lets you add a new value to the end of an array. Imagine that your array is a long wire with several connection points, and push() attaches a new connection at the end.

For example:

let colors = ["red", "green"];
colors.push("blue");
console.log(colors); // prints ["red", "green", "blue"]

Removing Elements with pop()

The pop() method removes the last element from an array. It “disconnects” the final connection point from the wire and returns the removed value.

For example:

let numbers = [1, 2, 3, 4];
let removed = numbers.pop();
console.log(numbers); // prints [1, 2, 3]
console.log(removed); // prints 4

Exercises

Creating and Printing an Array

  • Create a variable named colors using let.
  • Assign it an array containing the strings "red", "green", and "blue".
  • Print the entire array using console.log(colors).

Accessing and Updating an Array Element

  • Create a variable named numbers using let.
  • Assign it an array with the values [5, 10, 15].
  • Update the element at index 1 to 20.
  • Print the updated element using console.log(numbers[1]).

Creating an Empty Array and Filling It

  • Create a variable called animals using let.
  • Initialize it to an empty array.
  • Assign "cat" to index 0, "dog" to index 1, and "bird" to index 2.
  • Print the entire array using console.log(animals).

Adding an Element with push()

  • Create a variable called colors using let.
  • Assign it an array containing the strings "red" and "green".
  • Use the push() method to add "blue" to the array.
  • Print the updated array using console.log(colors).

Removing an Element with pop()

  • Create a variable called numbers using let.
  • Assign it an array containing [1, 2, 3, 4].
  • Use the pop() method to remove the last element and store it in a variable called removed.
  • Print both the updated array and the removed element using console.log.

hakatemia pro

Ready to become an ethical hacker?
Start today.

As a member of Hakatemia you get unlimited access to Hakatemia modules, exercises and tools, and you get access to the Hakatemia Discord channel where you can ask for help from both instructors and other Hakatemia members.