Skip to content Skip to sidebar Skip to footer

Why Can't I Remove The Intermediate Variable In My Code?

I'm currently working with the spread syntax and ran into an unexpected issue. The below snippet works (as expected), and doesn't throw any errors: However, if I remove the interm

Solution 1:

Add a semicolon and it works perfectly.

const arr = [1, 2, 3, 4];

[...arr].forEach(n => {
  console.log(n + 1);
});

The code was being evaluated without the newline - like this:

const arr = [1, 2, 3, 4][...arr]

Which resulted in your error.

Post a Comment for "Why Can't I Remove The Intermediate Variable In My Code?"