-2

When following an online tutorial, it works but when I’m doing it myself, mine does not read the HTML code within the single quote below; the error is:

app.js:15 Uncaught SyntaxError: Invalid or unexpected token.

obj.forEach(({ title, image, id, price }) => {
  results += '<div class="product">(a bunch of html code)';
});
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
nancyruya
  • 83
  • 7

1 Answers1

1

When you use "" or '', it works only for inline stuff. By inline stuff I mean you should open and close those guys on the same line.

const str = "<p> Hello World </p>"
const str2 = '<p> Bye World </p>'

But if you want a string with multiple lines, then use ``.

const str3 = `
<h1> Hello World </h1>
<p> We are ready to go! <p>
`;
  • What do you mean by “inline stuff”? ``const multilineString = "hello\n\`` ⟨linebreak⟩ `world";` certainly works. It’d be more accurate to say that linebreaks require escaping when using `''` or `""`. – Sebastian Simon Feb 19 '21 at 03:28