Nobody Wants to Use These JavaScript Array Methods (But You Should!)
JavaScript arrays come with many built-in methods that can make your life easier — if you know how to use them. However, some of these methods are either underused, misunderstood, or outright avoided by developers. In this article, we’ll dive into some of these “unloved” array methods, explore what they do, and show why they’re worth knowing.
Not a Medium member? Read this article here
1. Array.prototype.copyWithin()
This method copies part of an array to another location within the same array. While it’s rarely used, it can be helpful for rearranging elements without creating new arrays.
let arr = [1, 2, 3, 4, 5];
arr.copyWithin(0, 3); // [4, 5, 3, 4, 5]
console.log(arr);
Why use it?
It’s efficient when you need to shuffle or shift elements within an array in-place.
2. Array.prototype.fill()
fill()
replaces all or part of an array with a static value. Many developers overlook it, but it can be useful when creating placeholder data or resetting values.
let arr = [1, 2, 3, 4];
arr.fill(0); // [0, 0, 0, 0]
console.log(arr);