In this lesson you will
- Create arrays, read and change items by index and use length
- Add, remove and find items with push, pop, includes and indexOf
- Choose between for, for...of and while, and avoid endless loops
- Transform and total a list with map, filter and reduce
Arrays: ordered lists of values
An array holds several values in order inside one variable: const emirates = ["Abu Dhabi", "Dubai", "Sharjah"];. Each item has an index, starting at 0, so emirates[1] is "Dubai". emirates.length is 3, and because counting starts at 0, the last index is always length - 1. Asking for an index that does not exist, such as emirates[10], gives undefined rather than an error, which is a common source of silent bugs.
Arrays come with methods. push(item) adds to the end and pop() removes and returns the last item; unshift() and shift() do the same at the start. includes(value) answers whether a value is in the list, and indexOf(value) gives its position or -1 if it is missing. slice(1, 3) copies part of the array without changing it, and join(", ") turns it into one string. An array declared with const can still change in these ways, because the variable keeps pointing to the same array.
Console
- Console messages, errors, form data and clicked links appear here.
Index 0 is the first item. The last line prints undefined, not an error, because index 10 does not exist.
Sign in free to read the full lesson
You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.
Unlocks after sign-in:
- 2 more sections with live code examples
- Common mistakes to avoid
- Hands-on coding exercise with automatic checks
- Practice quiz and the graded lesson test
Free forever. No card, no paywall.
Questions people ask
Why do JavaScript arrays start at 0?
The index is the distance from the start of the array, so the first item is 0 steps away. Most programming languages that came before JavaScript, such as C and Java, count the same way, and JavaScript followed them.
What is the difference between for...of and for...in?
for...of gives you the values of an array, string or other iterable. for...in gives you the property names of an object, as strings. For arrays, use for...of; for...in is for objects, and even there Object.entries() is often clearer.
Should I use forEach or a for loop?
Both visit every item. for...of can stop early with break and works well with await, which you will meet later. forEach cannot be stopped early. Many teams use for...of for steps and map, filter and reduce when they build a new value.
How do I add up an array of numbers in JavaScript?
Either start a let total = 0; and add each value in a for...of loop, or use numbers.reduce((sum, n) => sum + n, 0). Always give reduce the starting value 0, so an empty array gives 0 instead of an error.
Why did my browser tab freeze when I ran a loop?
Almost always because the loop condition never became false, for example a while whose counter is never changed or a for whose step goes the wrong way. Close the tab, fix the line that should move the loop towards its end, and run again.
Sources
Facts in this lesson were checked against these official pages on 5 November 2026.
- MDN: Arrays (learn web development)
- MDN: Looping code
- MDN: Loops and iteration
- MDN: Array
- MDN: for...of
- MDN: Array.prototype.map()
- MDN: Array.prototype.filter()
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.