Welcome to our interactive tutorial on JavaScript Array Methods! 👋 Here, you'll find a comprehensive guide to essential array operations, explained with fun emoji examples. Each section includes a brief description, a code snippet, and an option to try the code yourself. Let's dive in and level up your JavaScript skills! 💪🚀
Adds one or more elements to the end of an array.
let livestock = ["🐷", "🐮", "🐔"];
livestock.push("🐴", "🐮");
console.log(livestock);
// ["🐷", "🐮", "🐔", "🐴", "🐮"]
Creates a new array from an array-like or iterable object.
const emoji = "🌈";
const rainbow = Array.from(emoji);
console.log(rainbow);
// ["🌈"]
Merges two or more arrays.
const fruit = ["🍎", "🍐"];
const veggies = ["🥕", "🥦"];
const healthyFood = fruit.concat(veggies);
console.log(healthyFood);
// ["🍎", "🍐", "🥕", "🥦"]
Tests whether all elements in the array pass the test implemented by the provided function.
const ages = [18, 21, 28, 34, 22];
const allAdults = ages.every(age => age >= 18);
console.log(allAdults);
// true
Changes all elements in an array to a static value.
let emotions = ["😊", "😢", "😠", "😍"];
emotions.fill("😐", 1, 3);
console.log(emotions);
// ["😊", "😐", "😐", "😍"]
Creates a new array with all elements that pass the test implemented by the provided function.
const fruits = ["🍎", "🍌", "🍇", "🍊", "🍓"];
const redFruits = fruits.filter(f => f === "🍎" || f === "🍓");
console.log(redFruits);
// ["🍎", "🍓"]
Creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.
const nestedFruit = ["🍎", ["🍌", "🍐"], [["🍊"]]];
const flatFruit = nestedFruit.flat(2);
console.log(flatFruit);
// ["🍎", "🍌", "🍐", "🍊"]
Determines whether an array includes a certain value among its entries.
const pets = ["🐶", "🐱", "🐠", "🐹"];
const hasDog = pets.includes("🐶");
console.log(hasDog);
// true
Creates and returns a new string by concatenating all of the elements in an array.
const elements = ["🌍", "🔥", "💨", "💧"];
const elementString = elements.join(" + ");
console.log(elementString);
// "🌍 + 🔥 + 💨 + 💧"
Creates a new array with the results of calling a provided function on every element in the array.
const hungryMonkeys = ["🐒", "🦍", "🦧"];
const feededMonkeys = hungryMonkeys.map(m => m + "🍌");
console.log(feededMonkeys);
// ["🐒🍌", "🦍🍌", "🦧🍌"]
Reverses an array in place.
let countdown = ["3️⃣", "2️⃣", "1️⃣", "🚀"];
countdown.reverse();
console.log(countdown);
// ["🚀", "1️⃣", "2️⃣", "3️⃣"]
Returns a shallow copy of a portion of an array into a new array object.
const rainbow = ["🔴", "🟠", "🟡", "🟢", "🔵", "🟣"];
const warmColors = rainbow.slice(0, 3);
console.log(warmColors);
// ["🔴", "🟠", "🟡"]
Tests whether at least one element in the array passes the test implemented by the provided function.
const drinks = ["🍵", "🧃", "🥤", "🍺", "🧉"];
const hasAlcohol = drinks.some(d => d === "🍺" || d === "🍷");
console.log(hasAlcohol);
// true
Sorts the elements of an array in place and returns the sorted array.
let fruits = ["🍓", "🍎", "🍌", "🍇"];
fruits.sort();
console.log(fruits);
// ["🍇", "🍈", "🍉", "🍊"]
Changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.
let seasons = ["☀️", "🍁", "❄️", "🌸"];
seasons.splice(2, 0, "🍂");
console.log(seasons);
// ["☀️", "🍁", "🍂", "❄️", "🌸"]
Adds one or more elements to the beginning of an array and returns the new length of the array.
let queue = ["🧍", "🧍", "🧍"];
queue.unshift("🧍♀️", "🧍♂️");
console.log(queue);
// ["🧍♀️", "🧍♂️", "🧍", "🧍", "🧍"]
Which method adds one or more elements to the end of an array?
Which method creates a new array with the results of calling a provided function on every element?
Which method executes a reducer function on each element of the array?