Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for breaking up long lines in JS [closed]

I've read several posts on this topic here, but I'm still uncertain how I should handle this issue.

In truth, the lines are in the source code much longer e.g.

console.log("html : "+"<li><a href=\""+el.find("link").text()+"\">"+el.find("title").text()+"</a>");

breaking it up in

console.log("html : "
  +"<li><a href=\""
  +el.find("link").text()
  +"\">"
  +el.find("title").text()
  +"</a>");

everything still works fine, but JSLint tells me "Bad line breaking before '+'"

What is the best practice, recommend way to keep the source human readable (production code will be minified).

like image 620
vbd Avatar asked Dec 09 '22 07:12

vbd


1 Answers

You must end line with +

Otherwise interpreters might treat it as the end of a line. ( thanks to Scimonster for explanation )

console.log('html: ' +
    '<li><a href="' +
    el.find('link').text() +
    '">' +
    el.find('title').text() +
    '</a>');

I recommend you to use single quotes in your JavaScript and double quotes in HTML. Then there is no need to escape double quotes, it also improves readability of your code.

like image 61
Andrew Surzhynskyi Avatar answered Dec 11 '22 10:12

Andrew Surzhynskyi