Can swapping newlines (e.g. by some text editor) break javascript code ?
views:
102answers:
2
+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
2010-02-02 20:27:59
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
2010-02-02 20:31:33
+1 for link to docs.
Mark Byers
2010-02-02 20:32:42
@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
2010-02-02 20:51:34
@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
2010-02-02 21:07:36
@Gumbo: I'm well aware that it is horrible practice to do so.
Georg
2010-02-02 21:17:35