Nested Quotation
   HOME

TheInfoList



OR:

A nested quotation is a
quotation A quotation is the repetition of a sentence, phrase, or passage from speech or text that someone has said or written. In oral speech, it is the representation of an utterance (i.e. of something that a speaker actually said) that is introduced by ...
that is encapsulated inside another quotation, forming a hierarchy with multiple levels. When focusing on a certain quotation, one must interpret it within its scope. Nested quotation can be used in literature (as in nested narration), speech, and computer science (as in "meta"-statements that refer to other statements as strings). Nested quotation can be very confusing until evaluated carefully and until each quotation level is put into perspective.


In literature

In languages that allow for nested quotes and use quotation mark punctuation to indicate direct speech, hierarchical quotation sublevels are usually punctuated by alternating between primary quotation marks and secondary quotation marks. For a comprehensive analysis of the major quotation mark systems employed in major writing systems, see Quotation mark.


In JavaScript programming

Nested quotes often become an issue using the eval keyword. The eval function is a function that converts and interprets a string as actual
JavaScript JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of websites use JavaScript on the client side for webpage behavior, of ...
code, and runs that code. If that string is specified as a literal, then the code must be written as a quote itself (and escaped accordingly). For example:
eval("var a=3; alert();");
This code declares a variable a, which is assigned the value 3, and a blank alert window is popped up to the user.


Nested strings (level 2)

Suppose we had to make a quote ''inside'' the quoted interpreted code. In JavaScript, you can only have ''one'' unescaped quote sublevel, which has to be the ''alternate'' of the top-level quote. If the 2nd-level quote symbol is the same as the first-level symbol, these quotes must be escaped. For example:
alert("I don't need to escape here");
alert('Nor is it "required" here');
alert('But now I do or it won\'t work');


Nested strings (level 3 and beyond)

Furthermore, (unlike in the literature example), the third-level nested quote must be escaped in order not to conflict with ''either'' the first- or second-level quote delimiters. This is true regardless of alternating-symbol encapsulation. Every level after the third level must be
recursively Recursion (adjective: ''recursive'') occurs when a thing is defined in terms of itself or of its type. Recursion is used in a variety of disciplines ranging from linguistics to logic. The most common application of recursion is in mathematics ...
escaped for all the levels of quotes in which it is contained. This includes the escape character itself, the backslash (“\”), which is escaped by itself (“\\”). For every sublevel in which a backslash is contained, it must be escaped for the level above it, and then all the backslashes used to escape that backslash as well as the original backslash, must be escaped, and so on and so forth for ''every'' level that is ascended. This is to avoid ambiguity and confusion in escaping. Here are some examples that demonstrate some of the above principles: document.write("

Hello, this is the body of the document."); document.writeln("

"); document.write("

A newline in HTML code acts simply as whitespace, whereas a <br> starts a new line."); document.write("

\n"); eval('eval(\"eval(\\\"alert(\\\\\\\"Now I\\\\\\\\\\\\\\\'m confused!\\\\\\\")\\\")\")');
Note that the number of backslashes increase from 0 to 1 to 3 to 7 to 15, indicating a rule for successively nested symbols, meaning that the length of the escape sequences
grows exponentially Exponential growth is a process that increases quantity over time. It occurs when the instantaneous rate of change (that is, the derivative) of a quantity with respect to time is proportional to the quantity itself. Described as a function, a q ...
with quotation depth.


See also

*
Embedded metalanguage In logic and linguistics, a metalanguage is a language used to describe another language, often called the ''object language''. Expressions in a metalanguage are often distinguished from those in the object language by the use of italics, quot ...
* Story within a story *
Play within a play A story within a story, also referred to as an embedded narrative, is a literary device in which a character within a story becomes the narrator of a second story (within the first one). Multiple layers of stories within stories are sometimes c ...
*
Leaning toothpick syndrome In computer programming, leaning toothpick syndrome (LTS) is the situation in which a quoted expression becomes unreadable because it contains a large number of escape characters, usually backslashes ("\"), to avoid delimiter collision. The officia ...


References

{{DEFAULTSORT:Nested Quotation Computer programming Syntax English grammar