Difference (if there is any) between ` and ' in javascript [duplicate]
Asked Answered
D

1

26

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?

Disdainful answered 12/11, 2015 at 19:31 Comment(5)
` is not an apostrophe. It is a grave accent mark.Endermic
See template strings developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…Subequatorial
Do you mean template strings developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…Inclusion
RelevantGrisly
Yup, template strings, that's it for `.Disdainful
A
48

' 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

Aquarelle answered 12/11, 2015 at 19:40 Comment(1)
Babel solved the back tick problem in IE11 for me:Attune

© 2022 - 2024 — McMap. All rights reserved.