Home > Net >  How spread operator working here with Array keys
How spread operator working here with Array keys

Time:08-25

Could someone explain how this [...Array(10).keys()]; working and logging 0 to 9 index?

console.log([...Array(10).keys()]);

CodePudding user response:

Here is the explanation

Case-1

const string = 'hello';

const array = [...string];

array;
// [ 'h' , 'e', 'l', 'l', 'o' ]

Case-2

Where we see spread operator acting as an eraser

[
  ...[1, 2, 3] // The dots erases the brackets
]

/*  Becoming this: */
[
  1, 2, 3 // "Erased"
]

Combining both of the above cases will help you on whats happening

  • Related