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, 'π₯'); // ["π", "π", "π₯", "π"]
If you liked this article, be sure to β€οΈ it.
Happy coding! β€οΈ
If you have any questions, you can contact me on Twitter.
You can find a ton of real-life tips and resources on my blog.
Top comments (7)
Great article!
What about Array.reduce()?
A good point. I'll add one example for it.
In the meantime, check this article:
markodenic.com/10-javascript-array...
Great examples!
Thanks, Andrew.
Perfect!
Thanks!