Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
15
rated 0 times [  16] [ 1]  / answers: 1 / hits: 22216  / 9 Years ago, thu, november 12, 2015, 12:00:00

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?


More From » apostrophe

 Answers
11

' 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.


[#64417] Tuesday, November 10, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
morrismilom

Total Points: 230
Total Questions: 96
Total Answers: 114

Location: Mayotte
Member since Mon, Sep 12, 2022
2 Years ago
;