Recently ran into some JS code that uses ` and '. I can't figure out if there is a different use for each apostrophe. Is there any?
Asked
Active
Viewed 2.3k times
25
-
2` is not an apostrophe. It is a grave accent mark. – Andrew Nov 12 '15 at 19:33
-
3See template strings https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/template_strings – elclanrs Nov 12 '15 at 19:33
-
2Do you mean template strings https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/template_strings – Ivan Nov 12 '15 at 19:33
-
[Relevant](http://stackoverflow.com/questions/29660381/backticks-calling-a-function) – Sterling Archer Nov 12 '15 at 19:34
-
Yup, template strings, that's it for `\``. – roscioli Nov 12 '15 at 19:36
1 Answers
44
'
or "
denotes a string
`
denotes a template string. Template strings have some abilities that normal strings do not. Most importantly, you get interpolation:
var value = 123;
console.log('test ${value}') //=> test ${value}
console.log(`test ${value}`) //=> test 123
And multiline strings:
console.log('test
test')
// Syntax error
console.log(`test
test`)
// test
// test
They have some other tricks too, more on template strings here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/template_strings
Note they are not supported in all javascript engines at the moment. Which is why template strings are often used with a transpiler like Babel. which converts the code to something that will work in any JS interpreter.

Alex Wayne
- 178,991
- 47
- 309
- 337