const newsletter = "Bytes" const tagline = "Your weekly dose of JavaScript" [newsletter, tagline].forEach((el) => console.log(el))
const newsletter = "Bytes" const tagline = "Your weekly dose of JavaScript" [newsletter, tagline].forEach((el) => console.log(el))
You might be surprised to learn that this code doesn’t execute. If you try, you’ll get an error – Uncaught ReferenceError: tagline is not defined. But clearly it is, right? Wrongo.
This is the very rare scenario where not using semicolons can bite you. Because we have a string followed by no semicolon followed by an opening array bracket, JavaScript interprets our code like this, as if we’re trying to access elements from the string.
"Your weekly dose of JavaScript"[newsletter, tagline].forEach();
This, of course, throws an error because tagline isn’t defined. To fix this, add a + sign before the array.
const newsletter = "Bytes" const tagline = "Your weekly dose of JavaScript" +[newsletter, tagline].forEach((el) => console.log(el))
That’s mostly a joke, but it does work…
Instead, just use semicolons.
const newsletter = "Bytes"; const tagline = "Your weekly dose of JavaScript"; [newsletter, tagline].forEach((el) => console.log(el));