Array.of

Fix for one of the JavaScript quirks

Array constructor in es5 and earlier has a curious quirk. The normal behavior is that if you call it with almost anything it will return a new array with just that element. Useful for converting any value into a single item array.

var array = Array(null);
console.log(array); // => [null]

But there’s an exception. If the argument happens to be a number instead of getting back an array with that element, something one would expect, it returns a new array with length set to that number.

var array = new Array(3);
console.log(array); // => [undefined × 3]

Granted, array constructor is not often used, as it’s more convenient to use array literal syntax []. But when one needs to do it, for example when using Ramda’s construct function, this unexpected behavior can cause problems.

Fortunately, this issue was recognized by es6 standard. Array class got a new method .of which implements expected behavior. Its usage is straightforward and should replace any

const array = Array.of(3);
console.log(array); // => [3]

Unfortunately, the old behavior must stay because of backwards compatibility.