NewJS
NewJS

Reputation: 55

How spread operator working here with Array keys

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

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

Upvotes: 2

Views: 69

Answers (1)

Amaarockz
Amaarockz

Reputation: 4684

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

Upvotes: 1

Related Questions