views:

102

answers:

2

Can swapping newlines (e.g. by some text editor) break javascript code ?

+4  A: 

Both control characters are valid line breaks. In fact you don’t even need line breaks. It’s just to for better readability. Except from situations where you really need those characters like in strings or regular expressions but there you can also use the escape sequences \n and \r.

Gumbo
I believe you can use line breaks in Javascript instead of semi-colons. In that case, removing them wouldn't be a good idea. :)
Georg
+1 for link to docs.
Mark Byers
@Georg - line breaks and semicolons are not interchangeable: see http://docstore.mik.ua/orelly/webprog/jscript/ch02_04.htm and http://magnetiq.com/2009/07/22/when-semicolons-are-not-optional-in-javascript/
Upper Stage
@Georg: The correct statement terminator is the semicolon. But in certain situations it is allowed to omit that terminator. Or to be more precise: The interpreter is allowed to insert a missing terminator in certain situations. (See http://bclary.com/2004/11/07/#a-7.9) But in general you should not do that. This concept of automatic semicolon insertion is considered as one of the worst “features” of ECMAScript.
Gumbo
@Gumbo: I'm well aware that it is horrible practice to do so.
Georg
+1  A: 

JavaScript is not white space sensitive. Important for regular expressions matching, though that may be beyond the scope of your question.

In fact it's not uncommon to compress JavaScript to remove all but the required breaks so that it downloads faster.

JustSmith