Skip to content

Array

In one line

An array is an ordered list of values stored under a single name, accessed by their position (index).

In simple words

An array lets you store multiple values in a single variable, in a specific order. Each value is called an element, and you access it by its position (index). In most languages, indexing starts at 0 — the first element is at index 0, the second at index 1, and so on.

Arrays are perfect for collections of related data: a list of scores, a set of user names, the pixels in an image. You can loop over an array to process every element, add or remove items, and search for specific values.

Arrays have a length (number of elements) and support operations like push (add to end), pop (remove from end), and slice (extract a portion). They are one of the most used data structures in programming.

Example

javascript
let fruits = ["apple", "banana", "cherry"];

console.log(fruits[0]);      // "apple" (index 0)
console.log(fruits.length);  // 3

fruits.push("date");
console.log(fruits); // ["apple", "banana", "cherry", "date"]

Arrays are indexed from 0. .length gives the count, and .push() adds an element to the end.

How it works

  1. Create an array with [value1, value2, ...].
  2. Access an element by index: arr[0] is the first element.
  3. Modify an element: arr[1] = newValue.
  4. Iterate with a loop: for (let i = 0; i < arr.length; i++).
  5. Use methods like push, pop, slice, map, filter to transform the array.

Related terms

Related Resources

Related Lessons

Related Projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary