RivoCode

JavaScript Fundamentals · Arrays · Lesson 16 of 48

Creating and Accessing Arrays

Concept

Creating and Accessing Arrays

Arrays store ordered lists of values, accessed by a zero-based index.

Because indexing starts at 0, the last item in an array sits at index length - 1, not at index length — a classic off-by-one spot to double check.

By the end of this lesson
  • Explain the core idea behind Creating and Accessing Arrays.
  • Predict output before running a change.
  • Test one realistic and one unusual input.
  • Use the result to make the next decision.
How to study this page
  1. 1. Read one concept.
  2. 2. Change the example.
  3. 3. Run, compare, and explain.
  4. 4. Complete the challenge below.
Syntax
const arrayName = [item1, item2, item3];
arrayName[index]
ExampleRunnable
const colors = ["red", "green", "blue"];
console.log(colors[0], colors.length);
Try it Yourself »
Accessing the last item safely
const colors = ["red", "green", "blue"];
console.log(colors[colors.length - 1]);
Note: colors.length - 1 always points to the last element, no matter how long the array is.
Self-check before continuing

Without looking at the example, describe what changes when you modify one input in Creating and Accessing Arrays. Then reopen the editor and prove your explanation with a small test.

You are ready to continue when you can predict, test, and explain the result.

Your turn

Create an array of 5 numbers and log the last one using its index.

Loading editor…

Console

Output will appear here...