DEV Community

loading...
Cover image for 14 JavaScript Array Methods (In 8 Minutes)

14 JavaScript Array Methods (In 8 Minutes)

Clean Code Studio
Clean Code Clean Life ~ Simplify
Updated on ・4 min read
cleancodestudio image

Twitter Follow

Did you know I have a newsletter? 📬

If you want to get notified when I publish new blog posts or make major project announcements, head over to https://cleancodestudio.paperform.co/

cleancodestudio image

[14 JS Array Methods (In 8 Minutes) Screencast]


let stocks = [
   { name: 'Apple', price: 321.85 },
   { name: 'Tesla', price: 2471.04 },
   { name: 'Disney', price: 118.77 },
   { name: 'Google', price: 1434.87 },
   { name: 'Netflix', price: 425.92 }
]
Enter fullscreen mode Exit fullscreen mode

Array.filter

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

stocks.filter(stock => stock.price < 1000) 
/*-------------------------------------------
 | Array.filter
 *-------------------------------------------
 |  0: {name: "Apple", price: 321.85}
 |  1: {name: "Disney", price: 118.77}
 |  2: {name: "Netflix", price: 425.92}
*/
Enter fullscreen mode Exit fullscreen mode

Array.map

The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.

stocks.map(stock => [stock.name, stock.price])
/*-------------------------------------------
 | Array.map
 *-------------------------------------------
 | 0: (2) ["Apple", 321.85]
 | 1: (2) ["Tesla", 2471.04]
 | 2: (2) ["Disney", 118.77]
 | 3: (2) ["Google", 1434.87]
 | 4: (2) ["Netflix", 425.92]
 */
Enter fullscreen mode Exit fullscreen mode

Array.find

The find() method returns the value of the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.

stocks.find(stock => stock.name === 'Tesla')
/*-------------------------------------------
 | Array.find
 *-------------------------------------------
 |  {name: "Tesla", price: 2471.04}
 */
Enter fullscreen mode Exit fullscreen mode

Array.some

The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn't modify the array.

stocks.some(stock => stock.price < 1000)
/*-------------------------------------------
 | Array.some
 *-------------------------------------------
 | true
 */

stocks.some(stock => stock.price < 10)
/*-------------------------------------------
 | Array.some
 *-------------------------------------------
 | false
 */

Enter fullscreen mode Exit fullscreen mode

Array.every

The every() method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.

stocks.every(stock => stock.price < 1000)
/*-------------------------------------------
 | Array.every
 *-------------------------------------------
 | false
 */

stocks.every(stock => stock.price < 2500)
/*-------------------------------------------
 | Array.every
 *-------------------------------------------
 | true
 */
Enter fullscreen mode Exit fullscreen mode

Array.forEach

The forEach() method executes a provided function once for each array element.

stocks.forEach(stock => console.log(stock))
/*-------------------------------------------
 | Array.forEach
 *-------------------------------------------
 | Outputs each item (stock object) from the array to the console
 | returns void (aka undefined)
 */
Enter fullscreen mode Exit fullscreen mode

Array.reduce

The reduce() method executes a reducer function (that you provide) on each element of the array, resulting in a single output value.

stocks.reduce((total, stock) => total + stock.price, 0)
/*-------------------------------------------
 | Array.reduce
 *-------------------------------------------
 | 4772.45
 */
Enter fullscreen mode Exit fullscreen mode
let names = ['Apple', 'Tesla', 'Disney', 'Google', 'Netflix']
Enter fullscreen mode Exit fullscreen mode

Array.includes

The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.

names.includes('Apple')
/*-------------------------------------------
 | Array.includes
 *-------------------------------------------
 | true
 */


names.includes('Microsoft')
/*-------------------------------------------
 | Array.includes
 *-------------------------------------------
 | false
 */
Enter fullscreen mode Exit fullscreen mode

Array.indexOf

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

names.indexOf('Tesla')
/*-------------------------------------------
 | Array.indexOf
 *-------------------------------------------
 | 1
 */
Enter fullscreen mode Exit fullscreen mode
names =  ['Apple', 'Tesla', 'Disney', 'Google', 'Netflix', 'Tesla']
Enter fullscreen mode Exit fullscreen mode

Array.lastIndexOf

The lastIndexOf() method returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at fromIndex.

names.lastIndexOf('Tesla')
/*-------------------------------------------
 | Array.lastIndexOf
 *-------------------------------------------
 | 5
 */
Enter fullscreen mode Exit fullscreen mode

Array.sort

The sort() method sorts the elements of an array in place and returns the sorted array. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.

The time and space complexity of the sort cannot be guaranteed as it depends on the implementation.

names.sort()
/*-------------------------------------------
 | Array.sort
 *-------------------------------------------
 | ['Apple, 'Disney', 'Google', 'Netflix', 'Tesla', 'Tesla']
 */
Enter fullscreen mode Exit fullscreen mode

Array.slice

The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.

names.slice(3)
/*-------------------------------------------
 | Array.slice
 *-------------------------------------------
 | ['Netflix', 'Tesla', 'Tesla']
 |
 */
Enter fullscreen mode Exit fullscreen mode

Array.join

The join() method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.

 names.join()
/*-------------------------------------------
 | Array.join
 *-------------------------------------------
 | "Apple,Disney,Google,Netflix,Tesla,Tesla"
 */

 names.join(' - ')
/*-------------------------------------------
 | Array.join
 *-------------------------------------------
 | "Apple - Disney - Google - Netflix - Tesla - Tesla"
 */


names.join('\\')
/*-------------------------------------------
 | Array.join
 *-------------------------------------------
 | "Apple\Disney\Google\Netflix\Tesla\Tesla"
 */
Enter fullscreen mode Exit fullscreen mode

Array.toString

The toString() method returns a string representing the specified array and its elements.

names.toString()
/*-------------------------------------------
 | Array.toString
 *-------------------------------------------
 | "Apple,Disney,Google,Netflix,Tesla,Tesla"
 */


stocks = [
   { name: 'Apple', price: 321.85 },
   { name: 'Tesla', price: 2471.04 },
   { name: 'Disney', price: 118.77 },
   { name: 'Google', price: 1434.87 },
   { name: 'Netflix', price: 425.92 }
]

stocks.toString()
/*-------------------------------------------
 | Array.toString
 *-------------------------------------------
 | "[object Object],[object Object],[object Object],[object Object],[object Object]""
 */

Enter fullscreen mode Exit fullscreen mode

Clean Code
Simplify Developer Lives


Did you know I have a newsletter? 📬

If you want to get notified when I publish new blog posts or make major project announcements, head over to

cleancodestudio image

Discussion (2)

Collapse
cleancodestudio profile image
Clean Code Studio Author

Checkout the Clean Code Studio screencast on JavaScript's Array.reduce function youtube.com/watch?v=WGleM3FeZmU

In my opinion, js's array reduce method is THE most powerful array function by a long shot. It's awesome :)

Collapse
bhargavsakaria profile image
Bhargav Sakaria

Please remove whatever kind of focus attribute there is on Email field in your newsletter form box. Any article i want to read I have to first scrollack upwards and then I can begin. Its irritating. No hard feelings. Your articles are amazing and helpful. Thank you.