An array stores multiple values in a single container

An array is a way to hold many pieces of data in one place, instead of creating a separate variable for each one. Think of it like a row of numbered boxes — each box holds one piece of information, and you can grab any box by its position number. Arrays are one of the most common tools in programming because they let you work with groups of related data efficiently.

Most programming languages create arrays in similar ways, though the exact syntax (the spelling and punctuation) differs. Once you understand the basic idea, you can explore it in JavaScript, Python, Java, or any other language you learn.

Key Takeaways

  • An array is created by declaring a variable and assigning it a list of values inside square brackets or parentheses, depending on your language.
  • Each item in an array has a position number called an index, starting at 0 for the first item, 1 for the second, and so on.
  • You retrieve a specific value from an array by writing the array name followed by the index number in square brackets.
  • Arrays can hold any type of data — numbers, text, true/false values — and in many languages, you can mix types in a single array.
  • Once created, you can add, remove, or change items in an array using built-in methods that your language provides.

Creating an array in JavaScript

In JavaScript, the simplest way to create an array is to write the variable name, an equals sign, and then the values inside square brackets, separated by commas. For example:

let fruits = ["apple", "banana", "orange"];

This creates an array called fruits that holds three text values. The square brackets tell JavaScript that this is an array. You can also create an empty array and add items to it later:

let colors = [];

This creates an empty array called colors. You can then add items one at a time using the index number, which starts at 0 for the first position.

Understanding array indexes and how to access items

Each item in an array has a position number called an index. The first item is always at index 0, the second at index 1, the third at index 2, and so on. This numbering starts at zero in almost every programming language, which surprises many people at first.

To get a value from an array, you write the array name followed by the index number in square brackets. If you have the array fruits = ["apple", "banana", "orange"], then fruits[0] gives you "apple", fruits[1] gives you "banana", and fruits[2] gives you "orange".

You can also change a value by using the same notation with an equals sign. For example, fruits[1] = "grape"; replaces "banana" with "grape" in the array.

Creating arrays in Python

Python uses the same square bracket notation as JavaScript, but Python calls arrays lists. The syntax is nearly identical:

fruits = ["apple", "banana", "orange"]

Notice that Python does not require the let keyword that JavaScript uses. You straightforward write the variable name, an equals sign, and the values in square brackets. Accessing items works the same way — fruits[0] returns "apple".

Python also lets you use negative indexes to count backward from the end of the list. fruits[-1] gives you the last item ("orange"), and fruits[-2] gives you the second-to-last item ("banana"). This is a Python-specific feature that makes working with the end of a list easier.

Adding, removing, and modifying array items

Once you create an array, you rarely leave it unchanged. Most languages provide built-in methods — pre-written commands — to add or remove items.

In JavaScript, push() adds an item to the end of an array. If you have fruits = ["apple", "banana"], then fruits.push("orange"); adds "orange" to the end, making the array ["apple", "banana", "orange"]. The pop() method removes the last item and returns it to you.

In Python, the equivalent command is append(). You write fruits.append("orange") to add an item to the end. To remove an item by its index, use pop() with the index number, like fruits.pop(1) to remove the item at index 1.

Both languages also let you insert items at a specific position. In JavaScript, use splice(). In Python, use insert() with the index and the value you want to add.

Working with arrays in loops

Arrays become powerful when you combine them with loops — code that repeats the same action multiple times. Instead of writing code to handle each item separately, you can write one block of code that runs once for each item in the array.

In JavaScript, a common loop is the for loop. This example prints each fruit in the array:

for (let i = 0; i < fruits.length; i++) { console.log(fruits[i]); }

This loop starts at index 0, repeats as long as the index is less than the array's length (the total number of items), and increases the index by 1 each time. The fruits.length property tells you how many items are in the array.

Python offers a simpler syntax for the same task:

for fruit in fruits: print(fruit)

This reads almost like English — "for each fruit in the fruits array, print it." Python handles the index counting for you automatically.

Arrays with different data types

Arrays do not have to hold only one type of data. In JavaScript and Python, you can mix numbers, text, true/false values, and even other arrays in a single array.

For example, in JavaScript:

let mixed = [42, "hello", true, 3.14];

This array holds a number, text, a boolean (true/false value), and a decimal number, all in one place. You access each item the same way — mixed[0] gives you 42, mixed[1] gives you "hello", and so on.

You can also create an array of arrays, sometimes called a multidimensional array. This is useful for storing data in rows and columns, like a spreadsheet:

let grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];

To access the number 5 in this grid, you would write grid[1][1] — the second row (index 1), second column (index 1).

Frequently Asked Questions

Why does array indexing start at 0 instead of 1?

This is a historical choice made in early programming languages and has stuck around. The index actually represents the distance from the start of the array — the first item is zero steps away, the second is one step away, and so on. Once you use it a few times, counting from 0 becomes automatic.

What happens if I try to access an index that does not exist?

In JavaScript, accessing an index beyond the array's length returns undefined, which means "no value." In Python, you get an error message that stops your program. Always check the array's length before accessing an index, or use a loop that handles this automatically.

Can I change the size of an array after creating it?

Yes. In both JavaScript and Python, arrays are dynamic, meaning they grow or shrink as you add or remove items. You do not need to decide the size when you create the array — you can start with an empty array and add items as needed.

What is the difference between an array and an object?

An array uses numbered indexes (0, 1, 2) to access items. An object uses named keys instead — like a dictionary where you look up a word by its name rather than its position. Arrays are better for ordered lists of similar items; objects are better for storing related information with descriptive labels.