Ron I Ron I - 6 months ago 32
Javascript Question

ES6 / ECMA6 template literals - not working

I wanted to try using template literals and it's not working. I am using Chrome v50.0.2 (and Jquery).

Example:

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


Output:

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


Hoping the SO community can help me sort this out, thanks in advance!

Answer

You are using the wrong quotations marks. You need to use backticks (next to the 1 key) not single quotes.

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: What is the usage of the backtick symbol (`) in JavaScript?