sage (@wavebeem) [archived]

TIL how JS Array concat determines "arrayness" of arguments

[].concat(1);
// [ 1 ]

[].concat([1, 2, 3]);
// [ 1, 2, 3 ]

[].concat({
  __proto__: Array.prototype,
  length: 1,
  0: "Hi"
});
// [ Array { '0': 'Hi', length: 1 } ]

[].concat({
  length: 1,
  0: "Hi",
  [Symbol.isConcatSpreadable]: true,
});
// [ 'Hi' ]
syntax highlighting by codehost

this differs from Array.from which will directly turn objects with length into arrays without checking for any other properties

#js#javascript#web dev#web development
easrng (@easrng) [archived]

want a sequence of numbers? just Array.from({length: 10}).map((e, i) => i*2) :)

#js#javascript#web dev#web development
mei 🌒& (@ckie) [archived]

this of course differs from Array(10).map((e, i) => i*2) which never runs the map lambda unless you first .fill the array.

#javascript#javascript is a normal programming language
easrng (@easrng) [archived]

yep! 😊

shuppy (@delan) [archived]

or my favourite, [...Array(10)].map((e, i) => i*2)