Best practice for breaking up long lines in JS

code-formatting, javascript

Solution

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.

Problem

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).

Original source