Skip to content

JavaScript Arrays and Loops: for, for...of, while, push, map and filter

NeedMyTools Academy

Sign in free to read the full lesson

Sign in or create a free account

Free forever. No card, no paywall.

A shopping cart, the prayer times for a week, the branches of a bank, the lines of an invoice: almost every real program works with lists. JavaScript stores lists in arrays and uses loops to do the same job for every item, whether there are three items or three thousand. Together they turn ten lines of copy and paste into three lines that never miss an item. This lesson covers both, plus the modern array methods that most professional code uses instead of hand-written loops.

Quick answer

Create an array with square brackets, const prices = [12, 8.5, 30];. Items are numbered from 0, so prices[0] is 12 and the last item is prices[prices.length - 1]. Add with push() and remove the last with pop(). To visit every item use for (const price of prices) { ... }; use a classic for (let i = 0; i < prices.length; i++) loop when you need the position, and while when you do not know how many times to repeat. map() makes a new array from each item, filter() keeps the items that pass a test, and reduce() combines them into one value.

30 min read and practice · Content checked on 5 November 2026

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.

Try it: A shopping list that changes
Code
ResultUpdates as you type
Console
  1. 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
Sign in or create a free account

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.

Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.