0

I'm using a spread operator with an HTMLCollection, to spread the HTMLCollection into an array. The problem is that I have to assign the new array to a new variable rather than using it directly with the forEach method.

[...h1.parentElement.children].forEach(function(el) {
   if(el !== h1) {
      el.style.transform = "scale(0.8)"
    }
})

The Above code produces that Error: Uncaught SyntaxError: Unexpected token '...' While it works when executing the below code.

const elArr = [...h1.parentElement.children]
elArr.forEach(function(el) {
   if(el !== h1) {
      el.style.transform = "scale(0.8)"
   }
})

Does anybody know why this behavior occurs?

Youssef
  • 1
  • 1
  • 1
    This is typically an [ASI](https://stackoverflow.com/questions/2846283/what-are-the-rules-for-javascripts-automatic-semicolon-insertion-asi) issue. Try prefixing your first line with `;` to see the difference – Phil Aug 05 '21 at 04:14
  • Thank you for your reply, now i understand what is wrong with the above code. – Youssef Aug 05 '21 at 05:42

0 Answers0