← 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