2

I was messing around with something today, where I'm returning a DOM tree. I was wondering if there was a way to have the code be like:

return
  '<div id="something"> \
     <p>Stuff</p> \
   </div>'

instead of:

return '<div id="something"> \
     <p>Stuff</p> \
   </div>'

just for aesthetic reasons - the first one looks better. I Googled it for about 10 minutes, then figured I ought to just ask those who know more than me.

Jonas
  • 121,568
  • 97
  • 310
  • 388
Connor
  • 4,138
  • 8
  • 34
  • 51

3 Answers3

7

No, it isn't.

A new line after a return triggers semi-colon insertion, so the code is equivalent to:

return;
  '<div id="something"> \
     <p>Stuff</p> \
   </div>';

…and you return undefined.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
2

I'm afraid not. Javascript sees the return-on-a-single-line and inserts a semicolon, ending the control flow.

Jens Roland
  • 27,450
  • 14
  • 82
  • 104
2

What comes closest to what you want is probably

return '\
   <div id="something"> \
       <p>stuff</p> \
   </div>';

Other then that I don't think it's possible

Johan
  • 1,537
  • 1
  • 11
  • 17