DEV Community

Cover image for JavaScript Arrays Explained: Creation, Indexing, and Common Methods
WISDOMUDO
WISDOMUDO

Posted on

JavaScript Arrays Explained: Creation, Indexing, and Common Methods

Arrays stand out as one of the most frequently used data structures in JavaScript.ย Whether you are working on a simple to-do list or building complex applications, arrays provide a reliable way to store, organize, and manipulate collections of data.

Working with arrays is a key skill in JavaScript, as they form the basis for handling collections of data, managing application state, and carrying out transformations. Once you understand how to create arrays, index elements, and use core methods, youโ€™ll be ready to solve problems youโ€™ll encounter in real projects.

What youโ€™ll learn

In this guide, youโ€™ll discover:

  • How to create arrays in different ways
  • How indexing works and why itโ€™s important
  • The most common methods used to manipulate arrays
  • Best practices to avoid common mistakes

By the end, youโ€™ll not only understand arrays but also be able to use them effectively in your own projects.

What is an Array?

Before exploring how to create arrays, itโ€™s important to understand what they are.

An array is an ordered list of values, with each element assigned a numeric index beginning at 0. In JavaScript, arrays are highly flexible and can store numbers, strings, objects, or even other arrays.

This flexibility makes arrays the go-to structure when you need to store lists of data. To make the most of arrays, itโ€™s important to first understand how to create them.

Creating Arrays

Arrays can be created in several ways, and choosing the right one depends on your use case.

Array Literals (recommended):

const colors = ['red', 'green', 'blue'];
๐Ÿ† Live Soccer TV: wisdomudo/javascript arrays explained creation indexing and common methods hlj - Jadwal Siaran Langsung ๐Ÿ† Live Soccer TV: wisdomudo/javascript arrays explained creation indexing and common methods hlj - Jadwal Siaran Langsung

The most common and readable way.

Array Constructor:

const numbers = new Array(3);   // [ <3 empty items> ]
๐Ÿ† Live Soccer TV: wisdomudo/javascript arrays explained creation indexing and common methods hlj - Jadwal Siaran Langsung ๐Ÿ† Live Soccer TV: wisdomudo/javascript arrays explained creation indexing and common methods hlj - Jadwal Siaran Langsung

Useful in rare cases, but can be confusing.

Array.of() and Array.from():

const single = Array.of(3);       // [3]
const chars = Array.from('wisdom'); // ['w','i','s','d','o', 'm']
๐Ÿ† Live Soccer TV: wisdomudo/javascript arrays explained creation indexing and common methods hlj - Jadwal Siaran Langsung ๐Ÿ† Live Soccer TV: wisdomudo/javascript arrays explained creation indexing and common methods hlj - Jadwal Siaran Langsung

Each creation method leads directly into how we access elements, which is where indexing comes in.

Indexing and Accessing Elements

Once youโ€™ve created an array, youโ€™ll often need to access specific items. Thatโ€™s where indexing comes into play.

Zero-based indexing:

const fruits = ['apple', 'banana', 'watermelon'];
console.log(fruits[0]); // 'apple'
๐Ÿ† Live Soccer TV: wisdomudo/javascript arrays explained creation indexing and common methods hlj - Jadwal Siaran Langsung ๐Ÿ† Live Soccer TV: wisdomudo/javascript arrays explained creation indexing and common methods hlj - Jadwal Siaran Langsung

Last element with length:

console.log(fruits[fruits.length - 1]); // 'watermelon'
๐Ÿ† Live Soccer TV: wisdomudo/javascript arrays explained creation indexing and common methods hlj - Jadwal Siaran Langsung ๐Ÿ† Live Soccer TV: wisdomudo/javascript arrays explained creation indexing and common methods hlj - Jadwal Siaran Langsung

Modern negative indexing with at():

console.log(fruits.at(-1)); // 'watermelon'
๐Ÿ† Live Soccer TV: wisdomudo/javascript arrays explained creation indexing and common methods hlj - Jadwal Siaran Langsung ๐Ÿ† Live Soccer TV: wisdomudo/javascript arrays explained creation indexing and common methods hlj - Jadwal Siaran Langsung

Indexing gives you control over array elements, making it possible to add, remove, or update values using built-in JavaScript methods.

Common Array Methods

Now that you can create arrays and access items, the next step is learning how to work with them efficiently. JavaScript provides many built-in methods for this purpose.

Adding and Removing Elements:

const nums = [1, 2];
nums.push(3);   // [1,2,3] - add to end
nums.pop();     // [1,2]   - remove from end
nums.shift();   // [2]     - remove from start
nums.unshift(0);// [0,2]   - add to start
๐Ÿ† Live Soccer TV: wisdomudo/javascript arrays explained creation indexing and common methods hlj - Jadwal Siaran Langsung ๐Ÿ† Live Soccer TV: wisdomudo/javascript arrays explained creation indexing and common methods hlj - Jadwal Siaran Langsung

Copying and Slicing:

const arr = [1, 2, 3, 4];
const part = arr.slice(1, 3); // [2,3]
๐Ÿ† Live Soccer TV: wisdomudo/javascript arrays explained creation indexing and common methods hlj - Jadwal Siaran Langsung ๐Ÿ† Live Soccer TV: wisdomudo/javascript arrays explained creation indexing and common methods hlj - Jadwal Siaran Langsung

Transforming Data:

const doubled = arr.map(x => x * 2); // [2,4,6,8]
const even = arr.filter(x => x % 2 === 0); // [2,4]
๐Ÿ† Live Soccer TV: wisdomudo/javascript arrays explained creation indexing and common methods hlj - Jadwal Siaran Langsung ๐Ÿ† Live Soccer TV: wisdomudo/javascript arrays explained creation indexing and common methods hlj - Jadwal Siaran Langsung

Reducing and Summarizing:

const sum = arr.reduce((a, b) => a + b, 0); // 10
๐Ÿ† Live Soccer TV: wisdomudo/javascript arrays explained creation indexing and common methods hlj - Jadwal Siaran Langsung ๐Ÿ† Live Soccer TV: wisdomudo/javascript arrays explained creation indexing and common methods hlj - Jadwal Siaran Langsung

These methods build upon the indexing concepts we covered earlier. For example, push appends an item to the end of an array, while slice generates a new array from a selected range of indexes.

Putting It All Together

Letโ€™s apply everything in one example. Imagine you have a list of orders and want to find the total of all paid amounts:

const orders = [
{ id: 1, amount: 20, status: 'paid' },
{ id: 2, amount: 0,  status: 'refunded' },
{ id: 3, amount: 50, status: 'paid' }
];
const totalPaid = orders
.filter(order => order.status === 'paid') // filter paid orders
.map(order => order.amount)               // extract amounts
.reduce((sum, amount) => sum + amount, 0); // calculate total
console.log(totalPaid); // 70
๐Ÿ† Live Soccer TV: wisdomudo/javascript arrays explained creation indexing and common methods hlj - Jadwal Siaran Langsung ๐Ÿ† Live Soccer TV: wisdomudo/javascript arrays explained creation indexing and common methods hlj - Jadwal Siaran Langsung

This short example ties together creation (arrays of objects), indexing (accessing properties), and methods (filter, map, and reduce) into a single, clear workflow.

Conclusion

Arrays go beyond simple lists; they serve as a core structure for managing data in JavaScript. Youโ€™ve learned how to create arrays, how to access items through indexing, and how to use common methods to manipulate and transform data.

The takeaway is clear: every concept stacks on top of the previous, creating a logical flow:

  • You start by creating arrays.
  • You then use indexing to access or modify elements.
  • Finally, you apply methods to transform data efficiently.

When you practice these concepts as a whole, youโ€™ll gain the ability to write JavaScript code that is cleaner, more effective, and efficient; useful for both small lists and large datasets.

You can reach out to me via LinkedIn

Top comments (0)