Register to get access to free programming courses with interactive exercises

Syntax JS: Arrays

An array in programming is any ordered set (or collection) of elements, whether they are courses on Hexlet, students in a group or friends on your favorite social network. The role of an array is to present such collections as a single structure, allowing you to work with them as a whole.

Array definition

// Creating an array
const items = [];

// Creating an array of three elements
const animals = ['cats', 'dogs', 'birds'];

In the example, the array ['cats', 'dogs', 'birds'] is defined, which is then assigned to the constant animals.

Pay attention to the names of constants containing arrays. They are plurals. This highlights the constant's nature and makes the code easier to analyse.

Getting data

Elements in the array are ordered from left to right. Each element has an ordinal number called an index. The array's indexing starts from zero. So the first element is available by index 0, the second by index 1, and so on. To extract an element by index from an array, a special syntax is used:

const animals = ['cats', 'dogs', 'birds'];
animals[0]; // 'cats'
animals[1]; // 'dogs'
// The last index in an array is always less than the size of the array by one
// This array has three elements, but the last index is two
animals[2]; // 'birds'

You can find out the size of an array by accessing its length property.

const animals = ['cats', 'dogs', 'birds'];
// Arrays have many other properties and techniques, which we will learn about during the courses
animals.length; // 3

In real-world tasks, the index is often calculated dynamically, so specific elements are accessed using variables:

let i = 1;
const animals = ['cats', 'dogs', 'birds'];
animals[i]; // 'dogs'

And even like this:

let i = 1;
let j = 1;
const animals = ['cats', 'dogs', 'birds'];
animals[i + j]; // 'birds'

Such a call is possible for one simple reason; an expression is expected inside the brackets, and where an expression is expected, you can substitute everything that is calculated. Including the function call:

const getIndexOfSecondElement = () => 1;
const animals = ['cats', 'dogs', 'birds'];
animals[getIndexOfSecondElement()]; // 'dogs'

Quite often when working with arrays you need to get the last element. To do this, just calculate the last index of the array using the formula array_size - 1, by which you can refer to the last element:

const animals = ['cats', 'dogs', 'birds'];
animals[animals.length - 1]; // 'birds'

at()

Another way to work with indexes is the at() method. It was added to be able to specify negative indexes, it allows you to take elements from the end without calculating indexes, like in the example above:

const animals = ['cats', 'dogs', 'birds'];
animals.at(0); // 'cats'
animals.at(1); // 'dogs'
// First from the end
animals.at(-1); // 'birds'
// Second from the end
animals.at(-2); // 'dogs'

Recommended materials

  1. Array documentation

For full access to the course you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

Bookmate
Health Samurai
Dualboot
ABBYY