Two ES6 array methods that don’t get any attention.

A look at Google Trends graph for fill, copywithin, foreach, and map.

ES6 introduced a couple of new array methods two of which are: .fill() and .copyWithin(). A quick look at Google Trends tells us two things:

  1. More people are learning to ditch for loops. (Increasing popularity of .map() and .forEach())
  2. Not too many people are interested in these two new ES6 array methods.

And the latter doesn’t seem to be changing very quickly either. The new methods seem to be trending pretty consistently and not gaining any popularity.

I’d guess this is partially because the new methods don’t have as many use cases as existing ones. But surely they have some use cases… right?! Otherwise they wouldn’t have been added to the language. Let’s take a look at how they work.


What it does.

Fills an array with a specified value. Yes, it mutates the original array.

let array = ["a","b","c"];  // ["a","b","c"]
array.fill("z"); // ["z","z","z"]
array; // ["z","z","z"]

MDN on .fill() .


What it does.

Lets you copy items from within an array to another location on an array. It takes up to three arguments: .copyWithin(target,[start],[end]) (bracket arguments are optional. Also mutates the original array.

let array = ["a","b","c"];  // ["a","b","c"]
array.copyWithin(0,1); // ["b","c","c"]
array; // ["b","c","c"]

My Thoughts

'javascript array fill' has had consistent search volume for about five years, well before the .fill() method was introduced. As ES6 becomes more prevalent, so probably will the use of .fill() . But for now there are so many stack overflow answers on how to solve specific array problems that people probably are going to be slow to adopt some of the more specific methods.