The question has been asked for many languages, yet not for javascript.
Ruby has the method Enumerable#each_cons
which look like that:
puts (0..5).each_cons(2).to_a
# [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5]]
puts (0..5).each_cons(3).to_a
# [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
How could I have a similar method in javascript for Array
?