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:

  1. function arithmetic(a, b) {
  2. return `Here is a table of arithmetic operations:
  3. ${a} + ${b} = ${a + b}
  4. ${a} - ${b} = ${a - b}
  5. ${a} * ${b} = ${a * b}
  6. ${a} / ${b} = ${a / b}`;
  7. }

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:

  1. const longString = 'This is a very long string that far exceeds the 80 \
  2. column limit. It unfortunately contains long stretches of spaces due \
  3. to how the continued lines are indented.';

Instead, write

  1. const longString = 'This is a very long string that far exceeds the 80 ' +
  2. 'column limit. It does not contain long stretches of spaces since ' +
  3. 'the concatenated strings are cleaner.';