Multi-line JavaScript

In a case of, "I didn't know you could do that," I discovered via some random blog post I've now lost track of, that you can do multi-line JavaScript by escaping the line breaks.

var a = 'You can start here \
and continue here \
and then finish it up here.';

The backslash on each line allows you to keep going. It's a little bit cleaner than opening and closing quotes and a concatenation operator on each line.

And now's your chance to say, "I can't believe you didn't know that."

Published May 05, 2009
Categorized as JavaScript
Short URL: https://snook.ca/s/945

Conversation

51 Comments · RSS feed
Marko Dugonjić said on May 04, 2009

— I can't believe you didn't know that.

There :)

Lim Chee Aun said on May 04, 2009

I can't believe you didn't know that. :P

Voyagerfan5761 said on May 04, 2009

Huh. I knew you could add or remove whatever whitespace you want, but I did not know that strings could be broken across lines using the backslash. Cool tip!

Mark Story said on May 04, 2009

I didn't know that either, so you're note the only one :) By the way, I think your quotes are mismatched.

twe4ked said on May 04, 2009

I can't believe you didn't know that.

Sorry, I didn't know it either, I just wanted to be the idiot to say it.

Have a good day!

Dustin Diaz said on May 04, 2009

I think you've been mistaken. The quote you're looking for is..." i can't believe i didn't know that either...."

This was like when I discovered when JavaScript had "for each in"
http://www.dustindiaz.com/for-each-in/

... yeah. (not to be confused with "array.forEach"

Jonathan Snook said on May 04, 2009

@Mark Story: Ooh, fixed mismatched quotes. Thanks!

Davi Ferreira said on May 04, 2009

I can't believe you didn't know that!!!

Stephanie Sullivan said on May 04, 2009

Well, the thing I most love about it is that you'll say it. Sometimes I don't blog things because I figure, "Oh heck, someone probably said that already 2 years ago... somewhere... probably." So good on ya because I sure didn't read it! :)

Chris Wallace said on May 05, 2009

That's like heredoc. I can't believe you didn't know that.

Anton said on May 05, 2009

JS on multiple lines... CSS on single lines... what the hell is the world coming to?!?
;)

Joy Dumb said on May 05, 2009

What is javascript? Who are you?
And what is this blog I keep hearing about on the fax?

simonth said on May 05, 2009

I can't believe you didn't know that.

Actually I do not know that too. Thanks for the tips. :)

philip said on May 05, 2009

neat trick, but JSLint doesn't like it. :)

Gustavs said on May 05, 2009

I can't belive you didn't know that.

yhss said on May 05, 2009

amazing!

Dave Waller said on May 05, 2009

I can't believe *I* didn't know that!

Drew McLellan said on May 05, 2009

Ah, I knew that once, but I'd totally forgotten it. Thanks for the reminder!

Travis said on May 05, 2009

Awesome tip Jonathon - I've always wondered if there was an easier way than the rather clumsy "close string, concatenate on each line". I probably should have looked it up... duh.

Adriaan said on May 05, 2009

mmm, Interesting... I wonder if this would work with JSminifier scripts?

Michael Kozakewich said on May 05, 2009

It struck me as odd when I first read this, because I distinctly remember not needing line breaks. Now that I think of it, though, I'm sure that was PHP. I'm sure I line-broke my javascript -- with quotes and semicolons.

saspijkerman said on May 05, 2009

— I can't believe I didn't know that.

cchana said on May 05, 2009

^ same! i had no idea u could do that! nice little tip and i think it will actually fix a problem i was having! :)

Jaap said on May 05, 2009

I did not know this, why hasn't anyone told me about this before?

Pete said on May 05, 2009

shucks, I can't believe I didn't know that either

Dominic said on May 05, 2009

Interesting. I don't think it is cleaner than concatenating several quoted strings though. Imaging you need to define such a long string inside some function - how do you indent it? Either your code looks ugly, or you have some extra spaces or tabs in your string.

village_idiot said on May 05, 2009

eh? What's my javascript?!

Beakid said on May 05, 2009

I can't \
believe you didn't know \
that

Jon Thomas said on May 05, 2009

Whether you knew it or not, there's always beginners like myself that don't know this stuff. Thanks for posting!

Jonathan Snook said on May 05, 2009

@Dominic: agreed, indentation could be a concern. I have that issue with the HEREDOC approach in PHP.

Teevio said on May 05, 2009

Yeah, I can't believe *I* didn't know that!

Garth Braithwaite said on May 05, 2009

I can't believe you didn't show this to us before.

Nick Sergeant said on May 05, 2009

The worse part of me not knowing about that, is that I've specifically searched for ways to do it. Oy.

Thanks!

Todd Austin said on May 05, 2009

I can't believe it's not butter... oh wait... o_O

joseanpg said on May 05, 2009

Yes, I can believe you didn't know that: it was illegal in ECMA-262 3rd Edition:

7.8.4 String Literals
...
NOTE
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 \n or \u000A.

But there is "good news", in the final draft of ECMAScript 5th (TC39/2009/025) that sintax becomes accepted:

7.8.4 String Literals
...
NOTE
A line terminator character cannot appear in a string literal, except when preceded by a backslash \ as a 'LineContinuation' to produce the empty character sequence. 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 \n or \u000A.

Juan said on May 05, 2009

I can't believe I didn't know that either. Damn all those '+'!

Anyway, I think it is good that we didn't know that. We shouldn't be writing too much text inside a script.

Brendon Kozlowski said on May 05, 2009

Different browsers support that slightly differently. As someone mentioned, I believe line indentation is different, and some browsers keep the newline within the string whereas others will remove it.

Unless you're using a specific browser (i.e.: Webkit in Adobe AIR or Titanium), then it might be more beneficial to simply use standard concatenation.

(I found out about this the other week so did a little further research. No specifics were given in my findings though, so perhaps they're outdated. I prefer to lean on the side of caution.)

Andrew Kolesnikov said on May 05, 2009

I can't belive you didn't know that, yes.

Andrew Kolesnikov said on May 05, 2009

*Still can't.*

Chris Akers said on May 05, 2009

YUI Compressor doesn't have any problems with it. It strips the return and the backslash.

Crockford's JSMin leaves the backslashes and returns as is.

+1 YUI

Bramus! said on May 05, 2009

Having written lots and lots of JS code, I didn't know it either. Thanks for the great tip :-)

caktux said on May 05, 2009

This random blog post was probably mine when I emailed you about it ;)

caktux said on May 05, 2009

That one more specifically : http://www.caktux.ca/blog/suy-jquery-ui-based-ie6-outdated-notification-plugin

I suppose you didn't need a "Your browser is outdated" jQuery notification for your too-hard-for-IE6 projects :)

Timothy Armstrong said on May 05, 2009

Believe it or not, I *did* know that, I just always found it to strange looking to use. Maybe you'll put it to more use than I have.

Keri Henare said on May 05, 2009

I can't believe that you didn't know that! But more importantly, I can't believe that I didn't know that!

Jonathan Snook said on May 05, 2009

@caktux: Actually, yes, I do believe that's what originally tipped me off. I remember thinking, "is this just for code presentation or does this actually work?" And it does. :)

pan69 said on May 05, 2009

I can't believe you didn't know that AND that you wrote a blog post about it. Amazing....

Will Anderson said on May 19, 2009

This brings back slightly disturbing memories of multi-line commands in classic asp :)

I actually ran into a problem with this yesterday and ended up using a slightly more convoluted solution, so thanks for pointing this out!

wiaderko said on May 20, 2009

lol
that was really simple :)

M.S. said on May 30, 2009

didn't know it either, been coding Javascript for 7 years :-(

Will Ayers said on July 03, 2009

Thanks for this, now some of the plugins I have been using make a lot more sense!

Sorry, comments are closed for this post. If you have any further questions or comments, feel free to send them to me directly.