Interesting JavaScript string capability
Reading through ECMA-262 and taking a look at Rhino‘s source code, I came across something interesting. Apparently, it is possible to have a JavaScript string begin on one line of code and end on another line of code. For example:
var s /*:String*/ = "Test \ multi \ line."; alert(s);
This is perfectly valid; the interpreter just ignores the slash and the line break. The output from this code snippet is “Test multi line.” Just goes to show, you can write books on a topic and still not know everything about it.
Disclaimer: Any viewpoints and opinions expressed in this article are those of Nicholas C. Zakas and do not, in any way, reflect those of my employer, my colleagues, Wrox Publishing, O'Reilly Publishing, or anyone else. I speak only for myself, not for them.
Both comments and pings are currently closed.




6 Comments
ES 7.3 Line Terminators
…
"A line terminator cannot occur within any token, not even a string".
ES 7.8.4 String Literals
…
"A ‘LineTerminator’ character cannot appear in a string literal, even if preceded by a backslash . The correct way to cause a line terminator character to be part of the string value of a string literal is to use an escape sequence such as
or u000A".
Zeroglif on December 26th, 2006 at 6:47 am
Thanks for the quotes, but this isn’t the way it was implemented. I’ve tested in the major browsers and the code above works. I think part of the confusion comes from the definition of escaped characters in the spec:
ES 7.8.4 String Literals
"The CV of DoubleStringCharacter :: EscapeSequence is the CV of the EscapeSequence."
Nicholas C. Zakas on December 26th, 2006 at 12:27 pm
Nicolas, ECMAScript forbids a "LineTerminator" in string literal. That’s an old "extension" to the standard. More quotes?
Ancient quote from Bill Anderson (don’t beleive it, there is no "continuation character" in javascript):
"The backslash () is the escape character for JavaScript. When used at the end of a line, it acts as a line continuation character".
http://www.setupray.com/tech/Web/Web%20Programming%20Unleashed/ch19.htm
Peter Torr (Microsoft):
"That’s a back-compat feature. ECMAScript says that you can’t continue string literals over more than one line, but we allow it as an extension to the standard".
http://groups.google.com/group/microsoft.public.dotnet.languages.jscript/msg/6a0e9c3d9fb95722
Jim Ley + Richard Cornford + Michael Winter:
"It’s been generally possible for years, it’s still a bad idea, as Richard Cornford noted, it’s not part of ECMASCript, it’s an extension, and as it doesn’t add anything we can already do, there’s no point taking the risk".
http://groups.google.com/group/comp.lang.javascript/msg/025d61c3a69c469d
Zeroglif on December 26th, 2006 at 3:19 pm
Wow, thanks for the great resources. I’m not advocating using it, just saying I wasn’t aware that it was even possible.
Nicholas C. Zakas on December 26th, 2006 at 4:10 pm
Am I missing the point, or is Zeroglif?
He says that a ‘line terminator’ can’t appear in a literal string, but surely we aren’t trying to put one in a literal string – just the opposite, we don’t want the line seperator in the string, we want the string to behave as if there isn’t one there.
Bill Skipton on October 8th, 2007 at 1:17 am
@Bill – I believe the point Zeroglif is making is that ECMAScript defines a string to exist on a single line. When you hit Enter (or Return), you’re inserting a line terminator and forcing a new line. Putting a slash before that essentially negates it but technically it should be a syntax error.
Nicholas C. Zakas on October 8th, 2007 at 8:04 pm
Comments are automatically closed after 14 days.