views:

36

answers:

3

While looking through a 3rd party JavaScript API and example code I noticed the following declaration. Basically XML assigned to a string but they seem to split it up on multiple lines using '\', I was unaware this could be done in javascript. Could anyone provide some more details on how this works?

Thanks.

var PrimaryChannel = '<ChannelParams ChannelType="Digital"> \
                                <DigitalChannelParams \
                                    PhysicalChannelIDType="Cable" \
                                    PhysicalChannelID="107" \
                                    DemodMode="QAM256" \
                                    ProgramSelectionMode="PATProgram" \
                                    ProgramID="2"> \
                                </DigitalChannelParams> \
                            </ChannelParams>';
+2  A: 

most browsers support this. it's not standards-compliant however

Scott Evernden
+3  A: 

It is escaping the newline character, but it's not recommended. If you minify your js after the fact, it will break horribly.

You are better off doing something like

var myString = 
   ['line1',
    'line2',
    'line3',
    'line4',
    'line5'].join('\n');

or

var mystring = 
    'line1' + 
    'line2' + 
    'line3' + 
    'line4' + 
    'line5';
Chad
+1  A: 

Yes, you can do that. \ is the line continuation character in JavaScript.

Edit: It's technically an escape character.

SammySP