JavaScript Array Methods

JavaScript Array Methods

Array methods

JavaScript Array Methods

JavaScript provides powerful built-in methods to manipulate arrays efficiently. Let’s explore the most commonly used array methods with examples! 

1️⃣ Adding & Removing Elements

🔹 push() – Add to the end

let arr = [1, 2, 3]; arr.push(4); console.log(arr); // [1, 2, 3, 4]

🔹 pop() – Remove from the end

let arr = [1, 2, 3]; arr.pop(); console.log(arr); // [1, 2]

🔹 unshift() – Add to the beginning

let arr = [2, 3, 4]; arr.unshift(1); console.log(arr); // [1, 2, 3, 4]

🔹 shift() – Remove from the beginning

let arr = [1, 2, 3]; arr.shift(); console.log(arr); // [2, 3]

2️⃣ Finding & Checking Elements

🔹 includes() – Check if an element exists

let arr = [1, 2, 3, 4]; console.log(arr.includes(2)); // true console.log(arr.includes(5)); // false

🔹 indexOf() – Find index of an element

let arr = [10, 20, 30, 40]; console.log(arr.indexOf(20)); // 1 console.log(arr.indexOf(50)); // -1 (not found)

🔹 lastIndexOf() – Find last occurrence

let arr = [1, 2, 3, 2, 1]; console.log(arr.lastIndexOf(2)); // 3

🔹 find() – Get first matching element

let arr = [5, 10, 15, 20]; let result = arr.find(num => num > 10); console.log(result); // 15

🔹 findIndex() – Get index of first match

let arr = [5, 10, 15, 20]; let index = arr.findIndex(num => num > 10); console.log(index); // 2

3️⃣ Iterating Over Arrays

🔹 forEach() – Loop through array

let arr = ["a", "b", "c"]; arr.forEach((item, index) => { console.log(`${index}: ${item}`); }); // Output: // 0: a // 1: b // 2: c

4️⃣ Transforming Arrays

🔹 map() – Create a new array by applying a function

let arr = [1, 2, 3]; let doubled = arr.map(num => num * 2); console.log(doubled); // [2, 4, 6]

🔹 filter() – Get a subset of the array

let arr = [10, 20, 30, 40]; let filtered = arr.filter(num => num > 20); console.log(filtered); // [30, 40]

🔹 reduce() – Reduce array to a single value

let arr = [1, 2, 3, 4]; let sum = arr.reduce((acc, num) => acc + num, 0); console.log(sum); // 10

🔹 some() – Check if at least one element satisfies condition

let arr = [3, 6, 9]; console.log(arr.some(num => num > 5)); // true console.log(arr.some(num => num > 10)); // false

🔹 every() – Check if all elements satisfy condition

let arr = [2, 4, 6]; console.log(arr.every(num => num % 2 === 0)); // true console.log(arr.every(num => num > 3)); // false

5️⃣ Modifying Arrays

🔹 sort() – Sort array (mutates original)

let arr = [40, 100, 1, 5, 25]; arr.sort((a, b) => a - b); // Ascending order console.log(arr); // [1, 5, 25, 40, 100]

🔹 reverse() – Reverse array

let arr = [1, 2, 3]; arr.reverse(); console.log(arr); // [3, 2, 1]

🔹 slice() – Extract a portion (does NOT mutate)

let arr = [1, 2, 3, 4, 5]; let sliced = arr.slice(1, 4); console.log(sliced); // [2, 3, 4]

🔹 splice() – Modify array (mutates)

let arr = [1, 2, 3, 4, 5]; arr.splice(2, 1, 99); // Remove 1 element at index 2 and insert 99 console.log(arr); // [1, 2, 99, 4, 5]

6️⃣ Joining & Splitting Arrays

🔹 join() – Convert array to string

let arr = ["apple", "banana", "cherry"]; console.log(arr.join(", ")); // "apple, banana, cherry"

🔹 split() – Convert string to array

let str = "hello world"; let arr = str.split(" "); console.log(arr); // ["hello", "world"]

7️⃣ Creating & Filling Arrays

🔹 Array.from() – Create array from iterable

let str = "hello"; let arr = Array.from(str); console.log(arr); // ["h", "e", "l", "l", "o"]

🔹 Array.of() – Create array from arguments


let arr = Array.of(10, 20, 30); console.log(arr); // [10, 20, 30]

🔹 fill() – Fill an array with a value

let arr = new Array(5).fill(0); console.log(arr); // [0, 0, 0, 0, 0]

8️⃣ Flattening & Concatenating Arrays

🔹 concat() – Merge multiple arrays

let arr1 = [1, 2]; let arr2 = [3, 4]; let merged = arr1.concat(arr2); console.log(merged); // [1, 2, 3, 4]

🔹 flat() – Flatten nested arrays

let arr = [1, [2, [3, 4]], 5]; console.log(arr.flat(2)); // [1, 2, 3, 4, 5]

🔹 flatMap() – Map and flatten in one step

let arr = [1, 2, 3]; let result = arr.flatMap(num => [num, num * 2]); console.log(result); // [1, 2, 2, 4, 3, 6]

9️⃣ Checking if Value is an Array

🔹 Array.isArray()

console.log(Array.isArray([1, 2, 3])); // true console.log(Array.isArray("hello")); // false

🔟 Summary

Use push(), pop(), shift(), unshift() to add/remove elements
Use map(), filter(), reduce() for transformations
Use find(), some(), every() for condition checks
Use sort(), reverse(), splice() for modifications
Use join(), split(), concat() for array manipulations

🚀 Now you’re a pro at handling JavaScript arrays! Let me know if you need more examples. 😊

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close