Array
- In Turkish
- Dizi
- Pronunciation
- uh-RAY
In short
An array is an ordered collection of values stored under one name, where each item is accessed by its numeric position, called an index, usually starting at 0.
What is an array?
An array holds multiple values in a specific order, such as a list of usernames or a series of temperature readings. Instead of creating a separate variable for each item, you store them all in one array and refer to each item by its index. In most languages, including JavaScript, Python, Java, and C, the first item is at index 0, the second at index 1, and so on.
In low-level languages like C, an array is a fixed-size block of memory where the items sit right next to each other. Because every slot has the same size, the computer can jump straight to any index with simple arithmetic, so reading items[500] is just as fast as reading items[0], which is described as O(1), or constant time. Higher-level languages usually provide dynamic arrays, such as JavaScript arrays, Python lists, and Java's ArrayList, which grow automatically as you add items.
A row of numbered mailboxes is a good analogy for an array: every box has a number, the boxes are in order, and you can go directly to box 7 without opening the first six. Arrays are used almost everywhere in programming, from holding the rows returned by a database query to storing the pixels of an image. They are commonly processed with loops or with methods like map, filter, and reduce.
Arrays are often compared with linked lists and with objects or dictionaries. Adding or removing items at the start or in the middle of an array is slow, because all the later items must shift over, while a linked list can do this cheaply but cannot jump directly to an index. An object or dictionary, by contrast, stores values under named keys instead of numeric positions.
At a glance
Key takeaways
- An array stores an ordered list of values under one name.
- Items are accessed by index, which usually starts at 0.
- Reading an item by index is fast, O(1), no matter how large the array is.
- Inserting or removing items at the start or middle is slower because other items must shift.
- Dynamic arrays, like JavaScript arrays and Python lists, resize automatically.
Example
// Create an array of three strings
const fruits = ["apple", "banana", "cherry"];
console.log(fruits[0]); // "apple" (indexes start at 0)
console.log(fruits.length); // 3
fruits.push("date"); // add an item to the end
fruits[1] = "blueberry"; // replace an item by its index
// Build a new array by transforming each item
const upper = fruits.map((fruit) => fruit.toUpperCase());
console.log(upper); // ["APPLE", "BLUEBERRY", "CHERRY", "DATE"]Readers ask
Why do arrays start at index 0?
In languages like C, an index is the distance, or offset, from the start of the array in memory, so the first item is zero steps away. Most later languages kept this convention, although a few, such as Lua, MATLAB, and R, start counting at 1.
What is the difference between an array and a list?
The terms overlap. Classically, an array is a fixed-size block of contiguous memory, while a list can grow and shrink; Python's list and JavaScript's Array are actually dynamic arrays that combine fast index access with automatic resizing.
How do I check if a value is an array in JavaScript?
Use Array.isArray(value), which returns true only for arrays. The typeof operator doesn't help here, because it returns "object" for arrays as well as for plain objects.
Often compared
See also
- Data TypeProgramming Fundamentals, p. 14A data type is a classification that tells a program what kind of value a piece of data holds, such as a number or text, and which operations work on it.
- LoopProgramming Fundamentals, p. 35A loop is a control structure that repeats a block of code, either a set number of times, once for each item in a collection, or while a condition stays true.
- Big O NotationProgramming Fundamentals, p. 6Big O notation describes how an algorithm's running time or memory use grows as its input gets larger, focusing on the growth rate rather than exact speed.
- AlgorithmProgramming Fundamentals, p. 2An algorithm is a finite, step-by-step set of instructions for solving a problem or completing a task, such as sorting a list or finding the shortest route.
- VariableProgramming Fundamentals, p. 56A variable is a named storage location in a program that holds a value, such as a number or a piece of text, which the code can read and change as it runs.
- JSONBackend & APIs, p. 25JSON is a lightweight, text-based format for storing and exchanging structured data as key-value pairs and lists, readable by both humans and machines.
- Two PointersData Structures, p. 35The two pointers technique walks an array or list with two indexes moved by simple rules, turning many problems that seem to need nested loops into one pass.
- Sliding WindowData Structures, p. 29The sliding window technique solves problems on contiguous parts of an array or string by updating a window as it slides instead of recomputing each subarray.
Spotted a mistake or something missing on this page?Suggest an edit