Why does this code print Array(7) [ <7 empty slots> ]
(or an array of undefined
, 7 times) instead of an array with the single element 7
?
const a = [ 7 ];
console.log(new Array(...a));
CodePudding user response:
new Array(...a) is the same as saying new Array(7), which will create an empty array with 7 undefined spots.
CodePudding user response:
If you want to use spread in an array do it like this
console.log([...a])