October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

JavaScript Arrays for Beginners: Create, Read, and Work with Lists

Create an array, work with zero-based indexes and length, and learn when to use common JavaScript array methods.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A JavaScript array stores an ordered collection of values. Create one with square brackets, use zero-based indexes to read or change its items, and choose methods such as push(), map(), and filter() for common tasks.

How do I create an array in JavaScript?

For a typical array, write comma-separated values inside square brackets:

As an Amazon Associate I earn from qualifying purchases.

const colors = ["red", "green", "blue"];

This creates an Array object with three ordered elements. Arrays can hold strings, numbers, objects, or other arrays, and they can grow or shrink. When learning, keep values consistent—for example, a list of colors or scores—so the operations are easier to follow. See MDN’s guide to indexed collections.

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

How do I get or change an item?

Array positions are called indexes, and counting starts at zero. In colors, the first item is at index 0, the second at 1, and the third at 2.

console.log(colors[0]); // "red"
colors[0] = "purple";
console.log(colors[0]); // "purple"

Reading an index that has no element returns undefined rather than throwing an error:

console.log(colors[3]); // undefined

MDN’s JavaScript language overview explains array access and common operations.

What does an array’s length mean?

Use the length property to check an array’s indexed extent:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
console.log(colors.length); // 3

For a dense array like this one, length is the number of elements. More exactly, it is one greater than the highest array index. That distinction matters for sparse arrays—arrays with empty slots. Increasing an array’s length creates empty slots; it does not fill them with actual undefined values. Empty slots and elements explicitly set to undefined are different, and some methods treat them differently. Beginners usually do not need to create sparse arrays deliberately.

How do I add or remove items?

push() adds one or more items at the end of the existing array. pop() removes and returns its last item. Both change the original array.

const shopping = ["bread", "apples"];
shopping.push("tea");
console.log(shopping); // ["bread", "apples", "tea"]

const removed = shopping.pop();
console.log(removed); // "tea"
console.log(shopping); // ["bread", "apples"]

These methods are convenient when you want to work at the end of a list. MDN’s Array reference documents them alongside other array methods.

Which method should I use: map, filter, find, or includes?

Use map() to transform every item

map() calls a function for each element and returns a new array containing the callback results. It does not replace the original array.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const prices = [2, 5, 8];
const doubled = prices.map(price => price * 2);
console.log(doubled); // [4, 10, 16]
console.log(prices);  // [2, 5, 8]

Use filter() to keep items that pass a test

filter() returns a new array containing the elements for which its callback returns a truthy result.

const scores = [42, 75, 90, 58];
const passing = scores.filter(score => score >= 60);
console.log(passing); // [75, 90]

Use find() or includes() to check for a match

find() returns the first matching element, or undefined if none matches. includes() checks whether an array contains a particular value and returns true or false.

const colors = ["red", "green", "blue"];
const firstLongColor = colors.find(color => color.length > 4);
console.log(firstLongColor); // "green"
console.log(colors.includes("blue")); // true

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

Choose map() when you need a new array of results. Choose forEach() to run an action for each element, such as printing it or updating another value; its useful result is not a transformed array.

const upperColors = colors.map(color => color.toUpperCase());

colors.forEach(color => console.log(color));

MDN’s Array documentation describes the behavior of these methods.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How do I go through every item?

Use a for...of loop when you want to perform an action for each value without needing its index:

for (const color of colors) {
  console.log(color);
}

Use a traditional for loop when the index is useful—for example, if you need to display a numbered item:

for (let index = 0; index < colors.length; index++) {
  console.log(index, colors[index]);
}

Both approaches visit the array in order. MDN’s language overview demonstrates array iteration.

Are arrays associative arrays?

No. Use nonnegative integer indexes to access array elements, not arbitrary string keys. As MDN puts it, “JavaScript arrays are not associative arrays and so, array elements cannot be accessed using arbitrary strings as indexes, but must be accessed using nonnegative integers (or their respective string form) as indexes.” See the MDN Array reference. If you need key/value lookup, an object or Map is generally a better fit than treating an array as a string-keyed collection.

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

Where can I learn more?

MDN’s free online guides and reference are useful for examples and looking up behavior as you learn. For a more guided print introduction, Nick Morgan’s JavaScript for Kids includes a dedicated chapter on arrays and practice challenges. The publisher presents it especially for children and parents, as well as beginners more broadly. It covers JavaScript beyond arrays, so it is an optional learning route, not a prerequisite. See the publisher’s book page.

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
Windows Errors? Fix Them Before They SpreadFree repair scan

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.