Disclaimer: This blog contains snippets of code, but you don't have to understand them to get the gist.
When I'm learning something new I often try to think about it in terms of a real world example. I usually find myself thinking 'how would I explain this to my parents?' This involves stripping out the jargon and making it relatable to someone who doesn't understand code. If you like football and you've ever tried explaining the offside rule with salt shakers, cups and cutlery, you'll know what I mean.
When my Mum and Dad do the Christmas cards, it's usually my Dad who does the writing, and my Mum who tells him who to write them for. We could write a very simple function to show this (if you only came here for the explanation, look away now):
const writeChristmasCard = name => {
console.log("Merry Christmas " + name + "!")
};
The function works something like this: Picture, if you can, my Mum in the living room, with the names. Dad is at the dining room table, with the cards. Mum shouts a name to him, he picks up one card and writes a nice message using that name. They have to repeat this for every single card:
writeChristmasCard("Spongebob");
// Merry Christmas Spongebob!
writeChristmasCard("Timothy");
// Merry Christmas Timothy!
writeChristmasCard("Uncle George");
// Merry Christmas Uncle George!
So all the cards are getting written, but its a long process that involves a fair bit of yelling.
If you were writing the cards, you'd probably have a list of people to send them to, right? In coding-speak, you would call this list an array. Let's get Mum to write the list as usual but rather than shout each name out in turn, walk into the dining room and give the list to my Dad so he can read it himself, and write a card for each name on that list. Quicker, and quieter! In code, the list would look something like this:
christmasList = [ "Spongebob", "Timothy", "Uncle George" ];
Each of those names is referred to as an 'element' of the array, so this list has 3 elements. There is no limit to the number of elements you can put in an array - the size (or length) of the array just depends on how many friends you have. You can give the array a title that describes it - in this case, 'christmasList' - then you can write a function which you pass that title to. The function can use this title to find out about everything contained within that array.
You can work with an array using a lot of different methods, but this one is nice because it's quite self explanatory. It abstracts away the mechanics but essentially it is 'iterating' over the array, or 'looking at each element and doing something to it':
const writeAllTheChristmasCards = christmasList => {
christmasList.forEach(name => {
console.log("Merry Christmas " + name + "!");
});
}
Returning to our example, Dad takes the list of names from Mum and iterates over it like so: he gets one Christmas card and writes the nice message using the first name on the list, then he does exactly the same for the next name, and for each subsequent name until he reaches the end of the list. By then he's got a lovely stack of nicely written cards that he finished in half the time and with much less shouting:
writeAllTheChristmasCards(christmasList);
// Merry Christmas Spongebob!
// Merry Christmas Timothy!
// Merry Christmas Uncle George!
To sum up... an array is a list of things, as opposed to one single thing. We can use arrays to perform repetitive tasks more efficiently.
If we want to actually deliver the cards we will need the addresses too, although an array isn't really suitable for that so stay tuned for the next thrilling instalment - 'Why you might want to use an object when writing your Christmas cards'
Top comments (0)