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."
Conversation
— I can't believe you didn't know that.
There :)
I can't believe you didn't know that. :P
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!
I didn't know that either, so you're note the only one :) By the way, I think your quotes are mismatched.
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!
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"
@Mark Story: Ooh, fixed mismatched quotes. Thanks!
I can't believe you didn't know that!!!
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! :)
That's like heredoc. I can't believe you didn't know that.
JS on multiple lines... CSS on single lines... what the hell is the world coming to?!?
;)
What is javascript? Who are you?
And what is this blog I keep hearing about on the fax?
I can't believe you didn't know that.
Actually I do not know that too. Thanks for the tips. :)
neat trick, but JSLint doesn't like it. :)
I can't belive you didn't know that.
amazing!
I can't believe *I* didn't know that!
Ah, I knew that once, but I'd totally forgotten it. Thanks for the reminder!
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.
mmm, Interesting... I wonder if this would work with JSminifier scripts?
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.
— I can't believe I didn't know that.
^ same! i had no idea u could do that! nice little tip and i think it will actually fix a problem i was having! :)
I did not know this, why hasn't anyone told me about this before?
shucks, I can't believe I didn't know that either
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.
eh? What's my javascript?!
I can't \
believe you didn't know \
that
Whether you knew it or not, there's always beginners like myself that don't know this stuff. Thanks for posting!
@Dominic: agreed, indentation could be a concern. I have that issue with the HEREDOC approach in PHP.
Yeah, I can't believe *I* didn't know that!
I can't believe you didn't show this to us before.
The worse part of me not knowing about that, is that I've specifically searched for ways to do it. Oy.
Thanks!
I can't believe it's not butter... oh wait... o_O
Yes, I can believe you didn't know that: it was illegal in ECMA-262 3rd Edition:
But there is "good news", in the final draft of ECMAScript 5th (TC39/2009/025) that sintax becomes accepted:
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.
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.)
I can't belive you didn't know that, yes.
*Still can't.*
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
Having written lots and lots of JS code, I didn't know it either. Thanks for the great tip :-)
This random blog post was probably mine when I emailed you about it ;)
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 :)
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.
I can't believe that you didn't know that! But more importantly, I can't believe that I didn't know that!
@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. :)
I can't believe you didn't know that AND that you wrote a blog post about it. Amazing....
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!
lol
that was really simple :)
didn't know it either, been coding Javascript for 7 years :-(
Thanks for this, now some of the plugins I have been using make a lot more sense!