October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

Understanding Arrays in JavaScript: A Beginner’s Guide

A practical beginner’s guide to JavaScript arrays: square-bracket syntax, zero-based indexes, length, and common methods that change or return arrays.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A JavaScript array is an ordered collection of values. Use square brackets to create one, a zero-based index to read an item, and the length property to find how many positions it has. Methods such as push(), pop(), map(), and filter() let you change or work with its contents.

What is an array in JavaScript?

An array stores values in a specific order, with each value accessible by its position, called an index. For example, an array can hold a list of colors or numbers. JavaScript arrays are indexed collections with a length property, as described in the MDN guide to indexed collections.

As an Amazon Associate I earn from qualifying purchases.

How do I create an array in JavaScript?

Write the values between square brackets and separate them with commas:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const fruits = ["apple", "banana", "cherry"];

This creates an array with three strings. You can also create an empty array and add values later:

const items = [];

For more examples and details, see the MDN Array reference.

How do I get the first item in an array?

Array indexes start at zero, so use array[0] for the first item, not array[1]:

const fruits = ["apple", "banana", "cherry"];
console.log(fruits[0]); // "apple"

The second item is at index 1, the third at index 2, and so on. For a non-empty, dense array, the last item is at index length - 1. An index equal to length is beyond the final position.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How do I find the length of an array?

Read the array’s length property:

const fruits = ["apple", "banana", "cherry"];
console.log(fruits.length); // 3
console.log(fruits[fruits.length - 1]); // "cherry"

length gives the number of array positions counted by the array, while the last-item expression above assumes the array is non-empty and dense. The ECMAScript 2025 specification defines arrays in terms of indexed properties and a length property.

How do I add or remove an item from an array?

For operations at the end of an array, push() adds values and pop() removes one. Both change the original array.

Append with push()

const fruits = ["apple", "banana"];
const newLength = fruits.push("cherry");
console.log(fruits); // ["apple", "banana", "cherry"]
console.log(newLength); // 3

push() returns the new length, not the value it added. The ECMAScript 2025 specification, section 23.1.3.23, defines it as appending its arguments to the array and returning the new length.

Remove the last item with pop()

const fruits = ["apple", "banana", "cherry"];
const removedFruit = fruits.pop();
console.log(removedFruit); // "cherry"
console.log(fruits); // ["apple", "banana"]

pop() returns the removed value and leaves the original array shorter.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What is the difference between map() and filter()?

Use map() when you want a result for every existing item; use filter() when you want to keep only items that pass a condition. Both return a new array rather than replacing the original.

Transform every item with map()

const numbers = [1, 2, 3];
const doubled = numbers.map((number) => number * 2);
console.log(doubled); // [2, 4, 6]
console.log(numbers); // [1, 2, 3]

Keep matching items with filter()

const numbers = [1, 2, 3];
const largeNumbers = numbers.filter((number) => number > 1);
console.log(largeNumbers); // [2, 3]
console.log(numbers); // [1, 2, 3]

A useful choice is: “Am I changing each item into a corresponding result?” If yes, use map(). “Am I selecting which items to keep?” If yes, use filter(). MDN’s Array reference documents these common array operations.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Do all array methods change the original array?

No. Check the behavior of the specific method instead of assuming all methods mutate the array or all preserve it. For example, push() and pop() change their array, while map() and filter() produce new arrays. slice() returns a portion in a new array; sort() changes the original array. These distinctions are documented method by method in the ECMAScript 2025 specification.

For numeric sorting, provide a comparator because the default sort does not order numbers numerically:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const values = [10, 2, 30];
values.sort((a, b) => a - b);
console.log(values); // [2, 10, 30]

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.