In JavaScript, a backtick† seems to work the same as a single quote. For instance, I can use a backtick to define a string like this:
var s = `abc`;
Is there a way in which the behavior of the backtick actually differs from that of a single quote?
† Note that among programmers, "backtick" is one name for what is more generally called the grave accent. Programmers also sometimes use the alternate names "backquote" and "backgrave". Also, on Stack Overflow and elsewhere, other common spellings for "backtick" are "back-tick" and "back tick".
Backticks enclose template literals, previously known as template strings. Template literals are string literals that allow embedded expressions and string interpolation features.
Template literals have expressions embedded in placeholders, denoted by the dollar sign and curly brackets around an expression, i.e.
${expression}
. The placeholder / expressions get passed to a function. The default function just concatenates the string.To escape a backtick, put a backslash before it:
Use backticks to more easily write multi-line string:
or
vs. vanilla JavaScript:
or
Escape sequences:
\u
, for example\u00A9
\u{}
, for example\u{2F804}
\x
, for example\xA9
\
and (a) digit(s), for example\251