What is the best practice for line continuation in JavaScript? I know that you can use \
for strings. But how would you split the following code?
var statement = con.createStatement("select * from t where (t.a1 = 0 and t.a2 >=-1) order by a3 desc limit 1");
Adding a backslash at the end of each line tells the JavaScript engine that the string will continue to the next line, thus avoiding the automatic semicolon insertion annoyance.
There are two ways to break JavaScript code into several lines: We can use the newline escape character i.e “\n”. if we are working on the js file or not rendering the code to the html page. We can use the <br> tag.
Sentences, entries, phrases, and clauses that continue in Area B of subsequent lines are called continuation lines. A hyphen in a line's indicator area causes the first nonblank character in Area B to be the immediate successor of the last nonblank character of the preceding line.
If I properly understood your question:
var statement = con.createStatement('select * from t where ' + '(t.a1 = 0 and t.a2 >=-1) ' + 'order by a3 desc limit 1');
For readability, it is fine to align +
operator on each row: Anyway, unless you're using Ecmascript 2015, avoid to split a multiline string with \
, because:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With