Complete Guide to JavaScript Arrays

Nevil BhayaniNevil Bhayani
1 min read

JavaScript Arrays: A Complete Guide

Arrays are one of the most important data structures in JavaScript.

Creating Arrays

const fruits = ['apple', 'banana', 'orange'];
const numbers = [1, 2, 3, 4, 5];

Array Methods

map()

The map() method creates a new array with the results of calling a function for every array element.

const doubled = numbers.map(x => x * 2);
console.log(doubled); // [2, 4, 6, 8, 10]

filter()

The filter() method creates a new array with all elements that pass a test.

const evenNumbers = numbers.filter(x => x % 2 === 0);
console.log(evenNumbers); // [2, 4]

Conclusion

Arrays are powerful and versatile. Master these methods and you'll be a JavaScript pro!

1
Subscribe to my newsletter

Read articles from Nevil Bhayani directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nevil Bhayani
Nevil Bhayani