Skip to main content

Chapter 4: Spellbooks

Arrays for Collections

You've learned to store single pieces of data in variables. But what if you have many items to keep track of? Enter arrays, the magical lists that can hold multiple values in a single variable. Think of an array as a spellbook – a collection of spells (or any items) kept together for easy use.

📚 What is an Array?

An array in JavaScript is a list-like structure enclosed in square brackets [], where each item in the list is separated by a comma. For example, let's make an array of spells:

let spells = ["Accio", "Expelliarmus", "Expecto Patronum", "Lumos"];

Here, we created a variable spells that holds an array of four string items. These are like four spells written in one spellbook (one array).

Arrays can hold numbers, strings, or any type of data, and you can mix types too (though it's usually best to keep them consistent). For instance:

let mixedBag = [42, "house points", true, "Niffler"];

This array has a number 42, a string "house points", a boolean true, and another string "Niffler". It's like a wizard's bag with a random assortment of items. But typically, you'll use arrays for a collection of similar things (all spells, all creatures, etc.).

🔢 Positions and Indexes

In a spellbook, spells are often numbered or ordered by pages. Similarly, items in an array have an order, starting from 0. Yes, zero. In programming, we count starting at 0 (it's a quirk that you'll get used to):

  • The first item is index 0.
  • The second item is index 1.
  • The third is index 2, and so on.

So in our spells array:

  • spells[0] is "Accio".
  • spells[1] is "Expelliarmus".
  • spells[2] is "Expecto Patronum".
  • spells[3] is "Lumos".

We use the bracket notation arrayName[index] to access an element. For example:

console.log(spells[1]);
// Output: Expelliarmus

This prints the second spell (because index 1 is the second item).

You can also assign new values using the same bracket notation:

spells[3] = "Lumos Maxima";
console.log(spells);
// Output: ["Accio", "Expelliarmus", "Expecto Patronum", "Lumos Maxima"]

We just updated the spell at index 3 (the 4th item) to a stronger version of Lumos. The array spells now reflects that change.

➕ Length of an Array and Adding Items

Every array has a property called length which tells how many items are in it:

console.log(spells.length);
// Output: 4 (since our spells array has 4 items)

If we want to add a new item to the array, one simple way is to use the push method:

spells.push("Wingardium Leviosa");
console.log(spells);
// Output: ["Accio", "Expelliarmus", "Expecto Patronum", "Lumos Maxima", "Wingardium Leviosa"]
console.log(spells.length);
// Output: 5

We used spells.push("Wingardium Leviosa") to add a new spell to the end of the array. Now the array has 5 spells, and length is 5.

Alternatively, if you know the exact position to put something (and that position is next in line), you could assign it directly by index:

spells[5] = "Avada Kedavra";

However, be careful: if you jump to an index that's not immediately after the last, you might create empty slots. Using push is safer to add to the end without skipping indexes.

🔄 Changing and Retrieving Items

We saw how to change an item by assigning to its index (like updating Lumos to Lumos Maxima). Retrieving is just reading arrayName[index] and using the value however you need – printing it, storing it in another variable, or passing it to a function.

let firstSpell = spells[0];
console.log("The first spell in my book is: " + firstSpell);
// Output: The first spell in my book is: Accio

Removing Items with .pop()

The .pop() method removes the last item from an array and gives it back to you:

let lastSpell = spells.pop();
console.log(lastSpell);
// Output: Wingardium Leviosa (the last item we pushed earlier)
console.log(spells.length);
// Output: 4 (one fewer now)

Finding Items with .indexOf() and .includes()

Need to know where a spell is in your spellbook? Use .indexOf():

let position = spells.indexOf("Expelliarmus");
console.log(position);
// Output: 1

If the item isn't in the array, .indexOf() returns -1 – a signal that nothing was found.

Just want to know if a spell exists in your collection without caring about position? Use .includes():

console.log(spells.includes("Lumos Maxima"));
// Output: true

console.log(spells.includes("Obliviate"));
// Output: false

🔤 String Spells – Useful String Methods

Strings have their own set of handy tricks, much like arrays. Here are some common ones every young wizard should know:

.length – Find out how many characters are in a string:

let spell = "Expelliarmus";
console.log(spell.length);
// Output: 12

.toUpperCase() and .toLowerCase() – Change the case of a string:

console.log(spell.toUpperCase());
// Output: EXPELLIARMUS

console.log(spell.toLowerCase());
// Output: expelliarmus

.includes() – Check if a string contains a certain piece of text:

let greeting = "Welcome to Hogwarts, young wizard!";
console.log(greeting.includes("Hogwarts"));
// Output: true

console.log(greeting.includes("Durmstrang"));
// Output: false

Notice that .includes() works on both arrays and strings – very versatile magic!

📝 Recap

  • Arrays store ordered lists of items in [] brackets.
  • Items are accessed by index, starting at 0.
  • Use .push() to add items, .pop() to remove the last item.
  • Use .indexOf() to find an item's position, .includes() to check if it exists.
  • Strings have useful methods too: .length, .toUpperCase(), .toLowerCase(), and .includes().

💡 Try It: Build Your Spellbook

  1. Create an array of at least 5 spells you'd want in your personal spellbook.
  2. Use console.log to print the third spell in your list.
  3. Add a new spell using .push().
  4. Check if "Avada Kedavra" is in your spellbook using .includes() (hopefully not!).
  5. Create a string variable with your wizard name and print it in all uppercase.

You now have the tools to manage collections and manipulate text like a true Codewarts student. In the next chapter, we'll learn how to make decisions in our code using if and else – it's time to choose your path!