Arrays are everywhere in JavaScript. Whether you’re building a simple to-do list or a complex web app, you’ll definitely work with them. So why not get smarter about it? In this cheatsheet, we’ll walk you through 13 incredibly useful and easy-to-understand JavaScript array tricks that can help improve your code instantly!

1. Remove Duplicates from an Array
Need to get unique values from an array? Use the Set
object to do it in one line!
2. Replace a Specific Value
Use splice()
to replace any item at a specific index.
3. Map Without .map()
You can also use Array.from()
with a mapping function to transform arrays.
4. Empty an Array
Want to clear out all items from an array? Set its length to 0.
5. Convert Array to Object
Use the spread operator to turn an array into an object with numeric keys.
6. Fill Array with Values
fill()
helps you create arrays filled with the same value.
7. Merge Multiple Arrays
Combine multiple arrays using the spread operator.
8. Find the Intersection of Two Arrays
Use filter()
and includes()
to get common elements.
9. Remove Falsy Values
Quickly filter out values like undefined
, null
, 0
, and ""
.
10. Get a Random Value
Select a random item from an array using Math.random()
.
11. Reverse an Array
Flip the order of items using reverse()
.
12. Find Last Index of a Value
Use lastIndexOf()
to find the last position of a value.
13. Sum All Values
Use reduce()
to sum all elements of a numeric array.
Final Thoughts
JavaScript arrays are powerful and flexible. These 13 tricks make your code cleaner and more efficient. Bookmark this cheatsheet and revisit it whenever you're working with arrays. Happy coding!