JS Learning

Arrays & Objects

Lesson Progress0%

Topics

Working with Arrays

Topic Overview

Take your time to understand each concept. Practice with the examples below!

Arrays are like ordered lists or containers that can hold multiple values. Think of them as a row of numbered boxes, where each box can store something different.

Real-world analogies:

  • A shopping list (items in order)
  • A line of people (first person, second person, etc.)
  • A playlist (song 1, song 2, song 3...)

Creating arrays:

const numbers = [10, 20, 30, 40];
const fruits = ["apple", "banana", "orange"];
const mixed = [42, "hello", true, null]; // Can mix types!

Accessing array items:

  • Arrays start counting at 0 (not 1!)
  • First item:arr[0]
  • Second item:arr[1]
  • Last item:arr[arr.length - 1]

Example:

const colors = ["red", "green", "blue"];
console.log(colors[0]); // "red" (first)
console.log(colors[2]); // "blue" (third)
console.log(colors.length); // 3 (total items)

Why arrays are useful:

  • Store multiple related values together
  • Process lists of data efficiently
  • Keep things in order
  • Perfect for collections of similar items

Ready to practice? Try the exercise below to reinforce your learning!

Try it yourself

Create an array of your favorite fruits and access the second element.

Loading...
1 / 4