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
🔹 pop()
– Remove from the end
🔹 unshift()
– Add to the beginning
🔹 shift()
– Remove from the beginning
2️⃣ Finding & Checking Elements
🔹 includes()
– Check if an element exists
🔹 indexOf()
– Find index of an element
🔹 lastIndexOf()
– Find last occurrence
🔹 find()
– Get first matching element
🔹 findIndex()
– Get index of first match
3️⃣ Iterating Over Arrays
🔹 forEach()
– Loop through array
4️⃣ Transforming Arrays
🔹 map()
– Create a new array by applying a function
🔹 filter()
– Get a subset of the array
🔹 reduce()
– Reduce array to a single value
🔹 some()
– Check if at least one element satisfies condition
🔹 every()
– Check if all elements satisfy condition
5️⃣ Modifying Arrays
🔹 sort()
– Sort array (mutates original)
🔹 reverse()
– Reverse array
🔹 slice()
– Extract a portion (does NOT mutate)
🔹 splice()
– Modify array (mutates)
6️⃣ Joining & Splitting Arrays
🔹 join()
– Convert array to string
🔹 split()
– Convert string to array
7️⃣ Creating & Filling Arrays
🔹 Array.from()
– Create array from iterable
🔹 Array.of()
– Create array from arguments
🔹 fill()
– Fill an array with a value
8️⃣ Flattening & Concatenating Arrays
🔹 concat()
– Merge multiple arrays
🔹 flat()
– Flatten nested arrays
🔹 flatMap()
– Map and flatten in one step
9️⃣ Checking if Value is an Array
🔹 Array.isArray()
🔟 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. 😊