Programming

How do I use an array?

Updated 2026-08-14

✓

Quick answer

An array is a data structure that stores a collection of elements, typically of the same type, allowing for indexed access to each element.

This guide explains how to create and manipulate arrays in various programming languages, including JavaScript, Python, and Java.

Steps

  1. 1

    Define the Array

    Choose the programming language and define your array using the appropriate syntax.

  2. 2

    Access Elements

    Use the index to access specific elements within the array.

  3. 3

    Modify Elements

    Change the value of an element by assigning a new value to its index.

  4. 4

    Iterate Over the Array

    Use a loop to perform actions on each element in the array.

Creating an Array

To create an array, you can use different syntax depending on the programming language. For example, in JavaScript, you can use square brackets: `let arr = [1, 2, 3];`. In Python, you can use a list: `arr = [1, 2, 3]`. In Java, you declare an array with a specific type: `int[] arr = new int[3];`.

Accessing Array Elements

Array elements can be accessed using their index. In most languages, indexing starts at 0. For example, `arr[0]` will return the first element of the array.

Modifying an Array

You can change the value of an array element by assigning a new value to a specific index. For example, `arr[1] = 5;` will change the second element to 5.

Looping Through an Array

You can loop through an array using a for loop. In JavaScript, it looks like this: `for (let i = 0; i < arr.length; i++) { console.log(arr[i]); }`.

Watch out for

  • Arrays in some languages have a fixed size once created, while others allow dynamic resizing.
  • Accessing an index that is out of bounds may lead to errors or unexpected behavior.

FAQ

What is the difference between an array and a list?

In many programming languages, an array has a fixed size and type, while a list can be dynamic and may contain different types of elements.

Can I store different types of data in an array?

In languages like JavaScript, yes, you can store different types in an array. However, in statically typed languages like Java, all elements must be of the same type.

How do I find the length of an array?

In JavaScript, use `arr.length`. In Python, use `len(arr)`. In Java, use `arr.length`.