Back to Kids Home
๐Ÿ“‹ Lesson 5

Arrays: Super Lists! ๐Ÿ“‹โœจ

Keep track of LOTS of things at once - like a shopping list, your favorite games, or your friend's names!

๐Ÿค” What is an Array?

An array is a special list that can hold many things together in order. Think of it like a row of lockers, each with a number!

๐Ÿฌ My Favorite Candies

#0
๐Ÿซ
Chocolate
#1
๐Ÿญ
Lollipop
#2
๐Ÿฌ
Gummy
#3
๐Ÿช
Cookie

Each item has a number (starting from 0!)

๐ŸŒ Arrays in Real Life

You see arrays everywhere!

๐Ÿ›’

Shopping List

Milk, eggs, bread, apples...

๐ŸŽต

Music Playlist

Song 1, Song 2, Song 3...

๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ

Class List

All the students in your class!

๐Ÿงฑ

Lockers

Locker 1, Locker 2, Locker 3...

๐Ÿ“…

Days of Week

Mon, Tue, Wed, Thu, Fri...

๐ŸŽฎ

High Scores

1st place, 2nd place, 3rd place...

โœจ Making an Array

Use square brackets [ ] to make an array:

// A list of fruits
let fruits = [ "apple" , "banana" , "orange" ]
// A list of numbers
let scores = [ 100 , 85 , 92 , 78 ]

๐Ÿคฏ Fun Fact: Counting Starts at 0!

In programming, we start counting from ZERO, not one! It's a bit weird at first, but you'll get used to it!

0

First!

1

Second!

2

Third!

3

Fourth!

๐ŸŽฏ Getting Items from an Array

Use the item's number (index) to get it:

let pets = [ "dog" , "cat" , "fish" , "bird" ]
[0]
๐Ÿ•
dog
[1]
๐Ÿฑ
cat
[2]
๐ŸŸ
fish
[3]
๐Ÿฆ
bird
pets[0] โ†’ "dog" ๐Ÿ•
pets[2] โ†’ "fish" ๐ŸŸ

๐Ÿ”„ Changing Items in an Array

You can change any item by using its number:

Before:

colors = ["red", "blue"]
red
blue

After: colors[1] = "green"

colors = ["red", "green"]
red
green

โž• Adding Items to an Array

Use push() to add new items at the end:

Start with:

๐ŸŽ apple
๐ŸŒ banana
fruits.push("orange")

Now you have:

๐ŸŽ apple
๐ŸŒ banana
๐ŸŠ orange โœจ

๐ŸŽฎ Arrays in Games!

Video games use arrays ALL the time!

๐ŸŽ’

Inventory

Array of all items you've collected!

๐Ÿ†

Leaderboard

Array of player scores, sorted highest first!

๐Ÿ‘พ

Enemies

Array of all the bad guys on screen!

๐Ÿ—บ๏ธ

Level Map

Arrays of arrays make a whole world!

๐Ÿ“ How Many Items? Array Length!

Use .length to count items:

let friends = [ "Emma" , "Liam" , "Sophia" ]
friends. length // This is 3!
๐Ÿ‘ง
๐Ÿ‘ฆ
๐Ÿ‘ง
=
3

โญ Remember This!

  • 1

    Arrays are lists that hold many things together

  • 2

    Use square brackets [ ] to make an array

  • 3

    Counting starts at ZERO (0), not one!

  • 4

    Use .push() to add new items to the end