0

I was coding a snake game in js, and I found a code in which there is so

    for (let i = snakeArr.length - 2; i>=0; i--) { 
        snakeArr[i+1] = {...snakeArr[i]};
    }

in the above code what does {...snakeArr[i]} means can anyone tell what it is and what's it's purpose what are it's pros and cons

This is the snake array

let snakeArr = [
    {x: 13, y: 15}
];
  • It's called [spread syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax). – BenM Jun 11 '21 at 12:02
  • `{...snakeArr[i]}` creates a (shallow) copy of the object stored at `snakeArr[i]` – Andreas Jun 11 '21 at 12:05

0 Answers0