Arrays and array methods
An array is an ordered list of values. Shopping carts, search results, chat messages and leaderboards are all arrays.
Creating and reading arrays
Adding and removing
The big three: map, filter, reduce
These methods take a callback function and return a new array or value. They don't change the original. Professional JavaScript uses them constantly.
reduce takes two arguments: a function (accumulator, item) => newAccumulator and a starting value (0 above).
Chaining
Because each method returns a new array, you can chain them into a pipeline:
More useful methods
Sorting numbers:
[10, 9, 1].sort()gives[1, 10, 9]because the default sort compares items as strings. Always pass a compare function:.sort((a, b) => a - b).
Spread and destructuring
Practice
Given a list of students, print the names of those who passed (score ≥ 50), sorted from highest score to lowest, and the class average.
1.What does [1, 2, 3].map(n => n * 2) return?
2.Which method returns a single value built from all items?