5.6 String literals
5.6.1 Use single quotes
Ordinary string literals are delimited with single quotes ('
), rather thandouble quotes ("
).
Tip: if a string contains a single quote character, consider using a templatestring to avoid having to escape the quote.
Ordinary string literals may not span multiple lines.
5.6.2 Template literals
Use template literals (delimited with `
) over complex stringconcatenation, particularly if multiple string literals are involved. Templateliterals may span multiple lines.
If a template literal spans multiple lines, it does not need to follow theindentation of the enclosing block, though it may if the added whitespace doesnot matter.
Example:
function arithmetic(a, b) {
return `Here is a table of arithmetic operations:
${a} + ${b} = ${a + b}
${a} - ${b} = ${a - b}
${a} * ${b} = ${a * b}
${a} / ${b} = ${a / b}`;
}
5.6.3 No line continuations
Do not use line continuations (that is, ending a line inside a string literalwith a backslash) in either ordinary or template string literals. Even thoughES5 allows this, it can lead to tricky errors if any trailing whitespace comesafter the slash, and is less obvious to readers.
Disallowed:
const longString = 'This is a very long string that far exceeds the 80 \
column limit. It unfortunately contains long stretches of spaces due \
to how the continued lines are indented.';
Instead, write
const longString = 'This is a very long string that far exceeds the 80 ' +
'column limit. It does not contain long stretches of spaces since ' +
'the concatenated strings are cleaner.';