Wednesday, June 5, 2024
85
rated 0 times [  92] [ 7]  / answers: 1 / hits: 68595  / 8 Years ago, mon, may 16, 2016, 12:00:00

I wanted to try using template literals and it’s not working: it’s displaying the literal variable names, instead of the values. I am using Chrome v50.0.2 (and jQuery).


Example


console.log('categoryName: ${this.categoryName}ncategoryElements: ${this.categoryElements} ');

Output


${this.categoryName}
categoryElements: ${this.categoryElements}

More From » template-literals

 Answers
12

JavaScript template literals require backticks, not straight quotation marks.


You need to use backticks (otherwise known as "grave accents" - which you'll find next to the 1 key if you're using a QWERTY keyboard) - rather than single quotes - to create a template literal.


Backticks are common in many programming languages but may be new to JavaScript developers.


Example:
categoryName="name";
categoryElements="element";
console.log(`categoryName: ${this.categoryName}ncategoryElements: ${categoryElements} `)

Output:
VM626:1 categoryName: name 
categoryElements: element

See:

Usage of the backtick character (`) in JavaScript


[#62163] Friday, May 13, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mckinley

Total Points: 15
Total Questions: 101
Total Answers: 94

Location: Liechtenstein
Member since Fri, Sep 11, 2020
4 Years ago
;