Nesting Quotes And The Escape Character
and not the end of it
There are two ways to accomplish this goal: nesting opposite quotes
and using a special character called the escape character
Nesting Opposite Quotes
String values can be surrounded by either single quotes or double quotes. Except in the case of event handlers which always surround their values in double quotes, which type of quote you use is entirely up to you.
Which type of quote you use to surround your nested string
is determined by the type of quote surrounding its containing string. A nested string is surrounded by the oppisite kind of quote which surrounds its containing string. I call this "quote swapping". In other words, if my outer string is contained by double quotes, my inner string must be contained by single quotes, and vice versa.
Nest single and double quotes as follows:
var beasty = "He said 'howdy', I said 'hi'."
var boys = 'He said "howdy", I said "hi".'
The Escape Character
It is legal to surround a nested string with the same type of quote as is surrounding its containing string as long as the escape character
var beasty = "He said \"howdy\", I said \"hi\"."
var boys = 'He said \'howdy\', I said \'hi\'.'
Escaping Carriage Returns
Although the next lesson covers much more efficient ways to break up a long string, I'll quickly explain how the escape character can be used to do the same thing. Place a single string on multiple lines using only one
set of quotes for the entire string. Anywhere you want to break the string, place the escape character ( \ ) at the end of the line, then a carriage return.
my name is Jenn\
This carriage-retun-escaping technique which simply breaks up a string in source code is not often used because it does not provide a way to concatenate
a string variable into the mix. But hey, somebody out there just might like to use it!
>>String Literals and String Variables
>>Nesting Quotes And The Escape Character
>>String Concatenation and String Operators
>>Creating Line Breaks
>>Strings To Numbers: Data Type Conversion
>>Strings and The Write Method