top of page

JavaScript for Kids: Arrays and Loops

Hey there, young coders! Ready to level up your JavaScript skills? Today we're going to learn about two super cool things: arrays and loops. When you put these two together, you can do some amazing stuff!


Arrays: Your Digital Collection Box


Remember arrays in our previous lesson? They're like magical boxes that can hold lots of things. Let's start with a fun example:


let candies = ["lollipop", "chocolate", "gummy bears", "licorice"];

This array called candies holds different types of candy. Cool, right?


Loops: Do It Again and Again!

Loops are like a merry-go-round for your code as we learned in our loops tutorial. They let you do the same thing over and over without writing it multiple times. There are different types of loops, but we'll focus on the for loop today.


The Magic of For Loops


A for loop has three parts:

  1. Where to start

  2. When to stop

  3. How to change each time


Here's what it looks like:

for (let i = 0; i < 5; i++) {
    console.log("I love coding!");
}

This will print "I love coding!" five times. Let's break it down:

  • let i = 0: Start at 0

  • i < 5: Keep going as long as i is less than 5

  • i++: Add 1 to i each time


Combining Arrays and Loops

Now for the fun part! We can use loops to go through each item in an array. This is called "iterating" over an array. Check this out:


let candies = ["lollipop", "chocolate", "gummy bears", "licorice"];
for (let i = 0; i < candies.length; i++) {
	console.log("I love " + candies[i] + "!");
}

This will print:

I love lollipop!

I love chocolate!

I love gummy bears!

I love licorice!


Cool, right? We used candies.length to know when to stop the loop, and candies[i] to get each candy.


Let's Get Creative!

  1. Counting Array: Make an array of numbers and use a loop to add them up.


let numbers = [2, 4, 6, 8, 10];
let total = 0;
for (let i = 0; i < numbers.length; i++) {
	total += numbers[i];
}
console.log("The total is: " + total);

2. Secret Message Decoder: Make an array of letters and use a loop to spell out a word.

let secretCode = ['H', 'E', 'L', 'L', 'O'];
let decodedMessage = "";

for (let i = 0; i < secretCode.length; i++) {
    decodedMessage += secretCode[i];
}

console.log("The secret message is: " + decodedMessage);

Challenge Time!

Can you make a loop that counts how many fruits in this array start with the letter 'a'?

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

Hint: You can use fruits[i].startsWith('a') to check if a fruit starts with 'a'.


Wrapping Up


Great job, coding superstars! You've learned how to use arrays and loops together in JavaScript. With these powers combined, you can do all sorts of cool things like:

  • Count things in lists

  • Find specific items in arrays

  • Create patterns and sequences


Keep practicing and experimenting. The more you play with arrays and loops, the more amazing things you'll be able to create!


Happy coding, kids!

Comments


Subscribe to WeCode newsletter

Receive News and Updates

Thanks for submitting!

  • Twitter
  • Facebook
  • Linkedin

© 2024 by WeCode. Proudly created with Wix.com

bottom of page