Skip to main content

Command Palette

Search for a command to run...

JavaScript Array methods explained

Updated
β€’6 min readβ€’View as Markdown
JavaScript Array methods explained
M
Software Engineer.

Let's start!

1. concat()

Returns a new array that is this array joined with other array(s) and/or value(s).

const fruits = ['πŸ‰', '🍎', 'πŸ’', '🍌'];
const vegetables = ['πŸ§…', '🌽', 'πŸ₯•', 'πŸ₯‘'];

// 1. concat()
const food = fruits.concat(vegetables); // ["πŸ‰", "🍎", "πŸ’", "🍌", "πŸ§…", "🌽", "πŸ₯•", "πŸ₯‘"]

2. copyWithin()

Copies a sequence of array elements within the array.

// 2. copyWithin()
const fruits = ['πŸ‰', '🍎', 'πŸ’', '🍌'];

const fruitsCopy = fruits.copyWithin(0, 2); // ["πŸ’", "🍌", "πŸ’", "🍌"]

3. every()

Returns true if every element in this array satisfies the testing function.

// 3. every()
const fruits = ['πŸ‰', '🍎', 'πŸ’', '🍌'];

const allBananas = fruits.every(fruit => fruit === '🍌'); // false

4. fill()

Fills all the elements of an array from a start index to an end index with a static value.

// 4. fill()
const fruits = ['πŸ‰', '🍎', 'πŸ’', '🍌'];

const lemons =  fruits.fill('πŸ‹'); // ["πŸ‹", "πŸ‹", "πŸ‹", "πŸ‹"]

5. filter()

Returns a new array containing all elements of the calling array for which the provided filtering function returns true.

// 5. filter()
const fruits = ['πŸ‰', '🍎', 'πŸ’', '🍌'];

const onlyBananas = ['πŸ‰', '🍎', '🍌', '🍌'].filter(fruit => fruit === '🍌'); // ["🍌", "🍌"]

6. find()

Returns the found element in the array, if some element in the array satisfies the testing function, or undefined if not found.

// 6. find()
const fruits = ['πŸ‰', '🍎', 'πŸ’', '🍌'];

const cherry = fruits.find(fruit => fruit === 'πŸ’'); // "πŸ’"

7. findIndex()

Returns the found index in the array, if an element in the array satisfies the testing function, or -1 if not found.

// 7. findIndex()
const fruits = ['πŸ‰', '🍎', 'πŸ’', '🍌'];

const cherryIndex = fruits.findIndex(fruit => fruit === 'πŸ’'); // 2

8. forEach()

Calls a function for each element in the array.

// 8. forEach()
const vegetables = ['πŸ§…', '🌽', 'πŸ₯•', 'πŸ₯‘'];

vegetables.forEach(vegetable => console.log(vegetable));
// "πŸ§…"
// "🌽"
//  "πŸ₯•"
//  "πŸ₯‘"

9. includes()

Determines whether the array contains a value, returning true or false as appropriate.

// 9. includes()
const vegetables = ['πŸ§…', '🌽', 'πŸ₯•', 'πŸ₯‘'];

const includesCorn = vegetables.includes('🌽'); // true
const includesTomato = vegetables.includes('πŸ…'); // false

10. join()

Joins all elements of an array into a string.

// 10. join()
const vegetables = ['πŸ§…', '🌽', 'πŸ₯•', 'πŸ₯‘'];

const vegetablesGroup = vegetables.join(''); // "πŸ§…πŸŒ½πŸ₯•πŸ₯‘"

11. map()

Returns a new array containing the results of calling a function on every element in this array.

// 11. map()
const vegetables = ['πŸ§…', '🌽', 'πŸ₯•', 'πŸ₯‘'];

const doubledVegetables = vegetables.map(vegetable => vegetable + vegetable); 
// ["πŸ§…πŸ§…", "🌽🌽", "πŸ₯•πŸ₯•", "πŸ₯‘πŸ₯‘"]

12. push()

Adds one or more elements to the end of an array, and returns the new length of the array.

// 12. push()
const fruits = ['πŸ‰', '🍎', 'πŸ’', '🍌'];

fruits.push('πŸ₯'); // ["πŸ‰", "🍎", "πŸ’", "🍌", "πŸ₯"]

13. reverse()

Reverses the order of the elements of an array in place.

// 13. reverse()
const fruits = ['πŸ‰', '🍎', 'πŸ’', '🍌'];

const fruits = ['πŸ‰', '🍎', 'πŸ’', '🍌'];

const reversedFruits = fruits.reverse(); // ["🍌", "πŸ’", "🍎", "πŸ‰"]

14. slice()

Extracts a section of the calling array and returns a new array.

// 14. slice()
const fruits = ['πŸ‰', '🍎', 'πŸ’', '🍌'];

fruits.slice(2); // ["πŸ’", "🍌"]

15. some()

Returns true if at least one element in this array satisfies the provided testing function.

// 15. some()
const fruits = ['πŸ‰', '🍎', 'πŸ’', '🍌'];

const bananaExists = fruits.some(fruit => fruit === '🍌')); // true

16. sort()

Sorts the elements of an array in place and returns the array.

// 16. sort()
const fruits = ['πŸ‰', '🍎', 'πŸ’', '🍌', 'πŸ‰', 'πŸ‰', 'πŸ’', '🍎', '🍌'];
fruits.sort(); // ["πŸ‰", "πŸ‰", "πŸ‰", "🍌", "🍌", "🍎", "🍎", "πŸ’", "πŸ’"]

17. splice()

Adds and/or removes elements from an array.

// 17. splice()
const fruits = ['πŸ‰', '🍎', 'πŸ’', '🍌'];

fruits.splice(2, 1, 'πŸ₯'); // ["πŸ‰", "🍎", "πŸ₯", "🍌"]

Initially created on Twitter:

If you liked this article, be sure to ❀️ it.

Happy coding! ❀️

M

Thanks for making life more easier!

T
Tan Ahmed5y ago

brilliant article

K

Definitely the kind of article you bookmark and end up keep coming back to. Thanks Marko!

1
M

Glad you like it, Kieran. Enjoy!

S

Great content as always, keep it up. I enjoy the way you simplify the code into easily digestible pieces.

Good Job!

1
M

Thanks a lot, Shamel!

S

Thank you! I have bookmarked this one! πŸ’― Keep up your great works! πŸ™Œ

2
M

Thank you, Savio. I will definitely try my best.