← JavaScript tutorial

JavaScript

Arrays

An array holds an ordered list of values in square brackets: const fruits = ["apple", "banana"]. Access items by their zero-based index (fruits[0] is "apple").

Example: map and filter, the two most-used array methods

const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map((n) => n * 2);
const evens = numbers.filter((n) => n % 2 === 0);

document.getElementById("output").textContent =
  "Doubled: " + doubled.join(", ") + " | Evens: " + evens.join(", ");

.map() transforms every item into something new and returns a brand-new array of the same length, here, each number becomes double itself. .filter() keeps only the items where the given function returns true, discarding the rest, here, only the even numbers survive. Neither method changes the original numbers array at all; both return a completely new one, which is a deliberate JavaScript convention worth internalizing early, since it avoids a whole category of bugs where code accidentally mutates data something else is still relying on.

Try it yourself
Ready to go further?

These free tutorials are a preview, our Frontend Development cohort covers it in depth, with a real project and a cohort alongside you. Closes Nov 30

Enroll in Frontend Development

Cookie preferences

Choose what you're comfortable with. You can change this any time from the footer.

Don't miss out

Ready to build something real?

Create a free account to enroll in a track, and get notified the moment a cohort opens.

Create free account