Creating Arrays in JavaScript
Table of Contents + β
In the previous lesson, we learned how to pass functions as arguments using callbacks. Now letβs learn how to store many values together in a single place using arrays.
π¦ What is an Array?
An array is an ordered list of values. Instead of creating a separate variable for every value, we keep them all together under one name. Each value in the array is called an item, and the items stay in the order we put them.
We create an array by writing the values between square brackets [], separated by commas.
const fruits = ["apple", "banana", "cherry"];
console.log(fruits); // ["apple", "banana", "cherry"]Here we made one variable called fruits that holds three values. The square brackets tell JavaScript that this is an array.
π¨ Arrays Can Hold Any Type
An array is not limited to one kind of value. It can hold strings, numbers, booleans, and even a mix of all of them.
const numbers = [10, 20, 30, 40];const mixed = ["Alex", 25, true, "developer"];
console.log(numbers); // [10, 20, 30, 40]console.log(mixed); // ["Alex", 25, true, "developer"]The numbers array holds only numbers, while mixed holds a string, a number, and a boolean all at once.
π’ Accessing Items by Index
Every item in an array has a position called an index. The index tells us where the item sits in the list. In JavaScript, the index starts at 0, not 1, so the first item is at index 0, the second is at index 1, and so on.
We read an item by writing the array name followed by the index in square brackets.
const fruits = ["apple", "banana", "cherry"];
console.log(fruits[0]); // "apple"console.log(fruits[1]); // "banana"console.log(fruits[2]); // "cherry"Letβs walk through how each line pulls out one item:
fruits[0]reaches the first item, so it prints"apple".fruits[1]reaches the second item, so it prints"banana".fruits[2]reaches the third item, so it prints"cherry".
| Item | Index | How to Access |
"apple" | 0 | fruits[0] |
"banana" | 1 | fruits[1] |
"cherry" | 2 | fruits[2] |
The index starts at zero
The first item lives at index 0. This trips up many beginners, so it is
worth remembering: counting positions in an array always begins from 0.
π The length Property
Every array knows how many items it holds. We find this out with the length property, which we write as the array name followed by .length.
const fruits = ["apple", "banana", "cherry"];
console.log(fruits.length); // 3Here fruits.length counts the items in the array. There are three items, so it prints 3.
Because the index starts at 0, the last item always sits at index length - 1. With three items, the last one is at index 2.
const fruits = ["apple", "banana", "cherry"];
console.log(fruits[fruits.length - 1]); // "cherry"Letβs read that last line from the inside out:
fruits.lengthgives3, the number of items.fruits.length - 1works out to2, the index of the last item.fruits[2]then reaches that item, so it prints"cherry".
βοΈ Changing an Item
We can replace any item by assigning a new value to its index. The old value is thrown away and the new one takes its place.
const fruits = ["apple", "banana", "cherry"];
fruits[1] = "mango";
console.log(fruits); // ["apple", "mango", "cherry"]Here we changed the item at index 1 from "banana" to "mango". The rest of the array stayed the same.
π const Arrays Can Still Be Modified
When we declare an array with const, we cannot reassign the variable to a brand new array. But we can still change the items inside it. The const keyword locks the variable to one array, not the contents of that array.
const scores = [85, 90, 78];
scores[0] = 100; // β
Correct: changing an item is allowedconsole.log(scores); // [100, 90, 78]
scores = [1, 2, 3]; // β Wrong: reassigning the whole array throws an errorLetβs compare what each line does to the const array:
scores[0] = 100changes the item at index0, which is allowed, so the array becomes[100, 90, 78].scores = [1, 2, 3]tries to point the variable at a whole new array, whichconstforbids, so it throws an error.
Mutate vs reassign
Changing an item inside a const array is called mutating, and it is allowed.
Pointing the variable at a completely new array is called reassigning, and
that is what const prevents.
π A Practical Example
Letβs say Sam is tracking quiz scores. We can keep all the scores in one array, read any score by its index, find out how many quizzes there were, and update a score after a recheck.
const scores = [72, 88, 95, 64];
console.log(scores[2]); // 95 β the third scoreconsole.log(scores.length); // 4 β four quizzes taken
scores[3] = 70; // a recheck bumped the last score upconsole.log(scores); // [72, 88, 95, 70]Letβs trace how each line uses the same array:
scores[2]reads the item at index2, the third score, which is95.scores.lengthcounts the items and reports4quizzes taken.scores[3] = 70updates the last score after a recheck, leaving[72, 88, 95, 70].
One array holds the whole list, and each piece of information comes from a clear, simple line of code.
β οΈ Common Mistakes to Avoid
| Mistake | Problem | Solution |
| Starting the index at one | fruits[1] skips the first item | Remember the first item is at index 0 |
| Accessing an index that does not exist | fruits[10] gives undefined | Stay within 0 to length - 1 |
| Confusing length with the last index | fruits[fruits.length] is always undefined | The last index is length - 1 |
π§ Try It Yourself!
- Create an array of three of your favorite foods using square brackets.
- Print the first and last item using their index numbers.
- Use
.lengthto print how many items the array holds. - Change the second item to a new value and print the whole array.
- Try accessing an index that does not exist and see that it returns
undefined.
π§© What Youβve Learned
- β
An array is an ordered list of values created with square brackets
[] - β Arrays can hold any type, including strings, numbers, and booleans
- β
We access items by index, and the index starts at
0 - β
The
lengthproperty tells us how many items an array holds - β We can change an item by assigning a new value to its index
- β
A
constarray can be mutated but not reassigned
π Whatβs Next?
Now that we can create and read arrays, letβs learn the built-in tools that make working with them powerful. Letβs continue to Array Methods.