Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line continuation characters in JavaScript

Tags:

javascript

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"); 
like image 888
xralf Avatar asked May 09 '12 11:05

xralf


People also ask

How do you continue a line in JavaScript?

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.

How break JavaScript code into multiple lines?

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.

What is a line continuation?

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.


1 Answers

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:

  1. It's not standard JavaScript
  2. A whitespace after that character could generate a parsing error
like image 144
Fabrizio Calderan Avatar answered Oct 06 '22 16:10

Fabrizio Calderan