An array stores multiple values in a single container

An array is a way to hold a list of items — numbers, words, or other data — all in one place instead of creating separate variables for each one. When you create an array, you tell your program "here is a collection of things" and give it a name. Then you can refer to each item by its position in the list.

The exact steps to create an array depend on which programming language you are using. JavaScript, Python, Java, and C# all have slightly different syntax — the specific words and symbols you type. But the core idea is the same: you declare the array, give it a name, and put your values inside.

Key Takeaways

  • An array is created by declaring a name, then placing your values inside square brackets or parentheses, depending on your language.
  • Each item in an array has a position number called an index, and most programming languages start counting from zero, not one.
  • You can create an empty array and add items to it later, or populate it with values when you first create it.
  • Arrays work best when all the items are the same type — all numbers, all text, or all the same kind of object.

Creating an array in JavaScript

In JavaScript, you create an array by typing a variable name, an equals sign, then your values inside square brackets, separated by commas. Here is the basic pattern:

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

This creates an array called fruits that holds three text values. The word let tells JavaScript you are declaring a new variable. If you want to create an empty array and add items later, use empty brackets: let fruits = []; Then you can add items one at a time using the array name and a position number in brackets, like fruits[0] = "apple";

Remember that JavaScript counts positions starting from zero. The first item is at position 0, the second at position 1, and so on. This is called the index. When you write fruits[0], you are asking for the item at index 0, which is "apple".

Creating an array in Python

Python uses the same square bracket syntax as JavaScript, but without the let keyword. You simply type the variable name, equals sign, and your values:

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

Python also counts from zero, so the first item is at index 0. You access items the same way: fruits[0] gives you "apple". To add a new item to an existing array in Python, use the append() method: fruits.append("grape") This adds "grape" to the end of the list.

Python also lets you create an array of numbers without typing each one. If you want the numbers 1 through 10, you can use numbers = list(range(1, 11)) The range() function creates a sequence, and list() turns it into an array.

Creating an array in Java

Java requires you to specify what type of data the array will hold. If you want an array of whole numbers, you write int (for integer). If you want text, you write String. Here is the pattern:

int[] numbers = {1, 2, 3, 4, 5};

The square brackets after int tell Java this is an array of integers. The curly braces hold the values. You can also create an empty array by specifying its size: int[] numbers = new int[5]; This creates space for 5 numbers, but they start as zero. Like JavaScript and Python, Java counts from zero.

If you want to add items to a Java array after creating it, you reference the index directly: numbers[0] = 10; This puts the value 10 at position 0. One important difference in Java: once you create an array with a specific size, you cannot make it larger. If you need a flexible list that grows, use an ArrayList instead.

Creating an array in C#

C# syntax is very similar to Java. You specify the data type, use square brackets, and put values in curly braces:

int[] numbers = {1, 2, 3, 4, 5};

You can also create an empty array with a set size: int[] numbers = new int[5]; C# also counts from zero. To add or change items, use the index: numbers[0] = 10;

Like Java, a C# array has a fixed size once created. If you need a list that grows dynamically, use a List<int> instead. The syntax is slightly different: List<int> numbers = new List<int>(); Then you can use the Add() method to add items: numbers.Add(10);

Common mistakes when creating arrays

The most frequent error is forgetting that arrays start counting at zero. If you create an array with 5 items, the positions are 0, 1, 2, 3, and 4 — not 1 through 5. Trying to access position 5 will cause an error because it does not exist.

Another common mistake is mixing data types. If you create an array meant to hold numbers, do not try to put text in it — or if you do, understand that your language may convert it or throw an error. In languages like Java and C#, the type is strict. In Python and JavaScript, the language is more forgiving, but mixing types often leads to unexpected behavior later.

A third mistake is assuming an array is empty when it is not, or assuming it has more items than it does. Always check the length of your array before looping through it or accessing a specific position. Most languages provide a length property or len() function to tell you how many items are in the array.

When to use an array versus other storage methods

Use an array when you have a collection of similar items and you need to access them by position. Arrays are fast and simple. If you need to add or remove items frequently, or if the size changes often, consider a list or vector instead — these are arrays that grow and shrink automatically.

If you need to look up items by a name or label rather than by position, use a dictionary or map. For example, if you want to store a person's age and look it up by their name, a dictionary is better than an array. Arrays are best when order matters or when you know the position of the item you want.

Frequently Asked Questions

Can I create an array with different types of data in the same array?

In Python and JavaScript, yes — you can mix numbers, text, and other types in one array. In Java and C#, no — you must declare a specific type. If you need mixed types in Java or C#, use a more flexible structure like an Object array or a generic List.

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

Most languages will throw an error called an "index out of bounds" exception. Your program will stop running. Always check the length of your array before accessing a specific position, or use a loop that counts up to the array length automatically.

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

In Java and C#, no — arrays have a fixed size. In Python and JavaScript, you can add items using methods like append() or by assigning to a new index. If you need a flexible size in Java or C#, use a List or ArrayList instead.

How do I loop through all the items in an array?

Use a for loop that counts from 0 to the array length minus 1. Most languages also support a for-each loop that automatically goes through each item without you managing the index yourself. This is safer because you cannot accidentally go past the end of the array.

What is the difference between an array and a list?

An array has a fixed size and stores items by position. A list is usually more flexible — it can grow and shrink, and it often has built-in methods to add, remove, and search for items. In practice, many people use the terms interchangeably, but technically a list is a more powerful version of an array.