DevAcademy
LearnJavaScriptJavaScript Arrays
BeginnerJavaScript

JavaScript Arrays

Learn how to store multiple values using arrays, access elements, modify data, and use common array methods.

Reading Time

15 min

Lesson

Lesson 13 of 48

What is an Array?

An array is a special object in JavaScript used to store multiple values in a single variable. Each value is called an element and is accessed using its index, starting from 0.

Creating an Array

Try it yourself — edit and run

Console Output

Click “Run” to see the console output here.

Accessing Array Elements

Array elements are accessed using their index. The first element is at index 0.

Access Elements

Try it yourself — edit and run

Console Output

Click “Run” to see the console output here.

Updating Array Elements

You can change any element by assigning a new value to its index.

Update an Element

Try it yourself — edit and run

Console Output

Click “Run” to see the console output here.

Array Properties

The length property returns the total number of elements present in an array.

Array Length

Try it yourself — edit and run

Console Output

Click “Run” to see the console output here.

Common Array Methods

MethodDescription
push()Adds an element to the end
pop()Removes the last element
shift()Removes the first element
unshift()Adds an element to the beginning
includes()Checks if a value exists
indexOf()Returns the index of an element
slice()Returns a portion of an array
splice()Adds or removes elements

Using Array Methods

Try it yourself — edit and run

Console Output

Click “Run” to see the console output here.

Remember

Arrays can store different data types including numbers, strings, booleans, objects, functions, and even other arrays.

Interview Questions

Quick Quiz

1. Which index represents the first element of an array?

2. Which method adds an element to the end of an array?

3. Which property returns the total number of elements?