Introduction and Lesson Objectives

Hello, future JavaScript star! Today, we're delving into the world of Lists and Arrays, in JavaScript. A list, much like a drawer, holds numerous values within a single container. By the end of this lesson, we will have mastered the essential tactics for efficiently crafting, accessing, and manipulating JavaScript lists.

Defining Lists: The Basics

Think of a list, or array, as a fleet of spaceships, where each ship carries a distinct item. So, you may ask, how does one assemble this space fleet? There are two methods:

  • Using the array constructor new Array():
// Define list of first four planets
const planets = new Array("Mercury", "Venus", "Earth", "Mars");
console.log(planets); // Prints: ["Mercury", "Venus", "Earth", "Mars"]
  • Using brackets []:
// Define list of next four planets
const morePlanets = ["Jupiter", "Saturn", "Uranus", "Neptune"];
console.log(morePlanets); // Prints: ["Jupiter", "Saturn", "Uranus", "Neptune"]

Both techniques swiftly create a list, so feel free to choose the one you prefer!

Navigating with Indexing

Indexes in lists are akin to celestial coordinates. Each item in a list has an index that starts from 0 — this is specific to JavaScript.

// Access third planet in the list
console.log(planets[2]);  // Prints: "Earth"

// Access first planet in the list
console.log(planets[0]);  // Prints: "Mercury"

// Find the index of 'Mars'
console.log(planets.indexOf("Mars"));  // Prints: 3

The .indexOf() function assists us in locating "Mars" within the planets list.

List Properties: length Property

Determining list length is analogous to counting stars in a constellation. JavaScript achieves this with the .length property:

// Check the number of planets
console.log(planets.length);  // Prints: 4

Easy and simple!

Assigning and Changing list Elements

Just like planets may change their positions in the universe, so can the elements inside a list. We can replace, add, or modify items inside our lists or arrays.

// Replace "Venus" with "Cybertron"
planets[1] = "Cybertron";
console.log(planets); // Prints: ["Mercury", "Cybertron", "Earth", "Mars"]

In the above example, we replaced "Venus", the second item in the list (at index 1), with "Cybertron". It's as simple as that!

What if we want to modify a value instead of completely replacing it? We can retrieve the item, make our modifications, and then put it back into the list.

// Append the string " - the Red Planet" to "Mars"
planets[3] += " - the Red Planet"; // using the += operator to append to the element
console.log(planets); // Prints: ["Mercury", "Cybertron", "Earth", "Mars - the Red Planet"]

In the above example, we updated the "Mars" element in the list, appending a string to it. The result is that "Mars" has been replaced with "Mars - the Red Planet".

Manipulating lists is a frequent operation in JavaScript. Thus, understanding how to assign and change array elements is essential for every JavaScript developer!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal