Arrays in JavaScript
Master array methods for powerful data manipulation.
Array Basics Review
Arrays are ordered collections of values. They can hold any type of data and are zero-indexed.
Try it yourself
Output
Adding and Removing Elements
Try it yourself
Output
The map() Method
Creates a new array by transforming each element.
Try it yourself
Output
Key Point:
map() always returns a new array with the same length. It doesn't modify the original array.
The filter() Method
Creates a new array with elements that pass a test.
Try it yourself
Output
The reduce() Method
Reduces an array to a single value by applying a function to each element.
Try it yourself
Output
Understanding reduce: The first parameter is the accumulator (running total), the second is the current element. The second argument to reduce() is the initial value.
Finding Elements
Try it yourself
Output
Sorting and Reversing
Try it yourself
Output
Warning:
sort() and reverse() modify the original array! Use spread [...arr] to create a copy first.
Challenge: Data Processing
Given an array of products, find all products that are in stock and under $50, then calculate the total value of those products.
Your solution
Output