With arrays in JavaScript, you can create and manage a variety of data structures. Whether for simple lists or more complex data processing – arrays are an indispensable element in your programming. In this tutorial, I will introduce you to the basics of arrays and show you how to use them effectively.
Key insights
- Arrays are lists of values that can be stored in variables.
- Accessing elements is done via indices that start at 0.
- With various methods, you can flexibly manipulate arrays.
- You can process arrays automatically using loops.
Step-by-Step Guide
What is an array?
Arrays are simply lists of values that you can store in a variable. You can think of them like a box into which you place different things (values). An array in JavaScript is defined using square brackets [].

Now you have an array with three elements: Peter, Anna, and Susi.
Accessing array elements
To access the elements of an array, you use the index. It is important to know that: The index starts at 0. This means that the first element has index 0, the second has index 1, and so on.
This gives you the value Peter. The last index of an array is always n-1, where n is the number of elements in the array. If you have three elements, the last index is 2 (3-1=2).
Automated processing of elements
To automatically read all elements of an array and, for example, display them, you can use a loop. Let’s say you want to output the animal names from a list.

In this loop, each animal name is appended to a string output, and
ensures that each name is displayed on a new line.
Array methods
There are various methods that help you process arrays. A commonly used method is.length, to determine the number of elements in the array.

If you want to sort the elements, you can use the.sort() method.
This sorts the animals in alphabetical order.
Adding new elements
Another important concept is adding new elements to an array. You can use the.push() method to add a new element to the end of the array.

Now you have added the animal cat. This means the array will now contain four elements.
Summary – Basics of working with arrays in JavaScript
You have now learned the most important basics about arrays in JavaScript. Arrays are a great way to store and manage data. You can easily access them via indices, use various methods to manipulate them, and utilize loops to efficiently read their contents.
Frequently Asked Questions
How do I start with an array in JavaScript?You start by defining an array between square brackets, e.g. let employees = [];.
How do I access the elements of an array?You access the elements via the index, with the first index being 0, e.g. employees[0].
How can I determine the number of elements in an array?With the.length method, e.g. employees.length.
How do I add a new element to an array?You use the.push() method, e.g. employees.push('Mark');.
How do I sort the elements of an array?Using the.sort() method, e.g. employees.sort();.