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?