JS Learning

Arrays and Objects

Lesson Progress0%

Section 2 of 4

Array Methods

Array Methods

Topic Overview

Take your time to understand each concept. Practice with the examples below!

Arrays come with built-in "methods" (functions) that help you manipulate and work with your data. These are like special tools in your array toolkit!

Adding and Removing Items:

  • push() - Add to the end
const fruits = ["apple", "banana"];
fruits.push("orange"); // ["apple", "banana", "orange"]
  • pop() - Remove from the end
fruits.pop(); // Removes "orange", returns it
  • unshift() - Add to the beginning
fruits.unshift("grape"); // ["grape", "apple", "banana"]
  • shift() - Remove from the beginning
fruits.shift(); // Removes "grape", returns it

Transforming Arrays:

  • map() - Create a new array by transforming each item
const numbers = [1, 2, 3];
const doubled = numbers.map(num => num * 2); // [2, 4, 6]
  • filter() - Create a new array with only items that pass a test
const ages = [12, 18, 25, 16, 30];
const adults = ages.filter(age => age >= 18); // [18, 25, 30]
  • reduce() - Calculate a single value from all items
const prices = [10, 20, 30];
const total = prices.reduce((sum, price) => sum + price, 0); // 60

These methods are like having assistants that can reorganize, transform, or analyze your lists automatically!

Ready to practice? Try the exercise below to reinforce your learning!

Try it yourself

Use array methods to: 1) Add a number to the end, 2) Filter even numbers, 3) Double all values.

Loading...