ECMAScript template literals like 'some ${string}' are not working

JavascriptTemplate Literals

Javascript Problem Overview


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}

Javascript Solutions


Solution 1 - Javascript

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:

https://stackoverflow.com/questions/27678052/what-is-the-usage-of-the-backtick-symbol-in-javascript

Solution 2 - Javascript

There are three quotation marks, but just one entrance is working which we can use as TEMPLATE LITERALS:

  1. " " (é key on keyboard) is not working:
console.log("Server is running on port: ${PORT}")
  1. ' ' (Shift + 2 key on keyboard) is not working:
console.log('Server is running on port: ${PORT}')
  1. ` ` (Alt + Num96 key on keyboard) is working:
console.log(`Server is running on port: ${PORT}`)

Screenshot of console.log(Server is running on port: ${PORT})

Solution 3 - Javascript

it only works if you use backpacks, on my Mac Pro that is ` which is above the tab key.

If you use single or double quotes it won't work!

Solution 4 - Javascript

// Example
var person = {
  name: "Meera",
  hello: function(things) {
    console.log(`${this.name} Says hello ${things}`);
  }
}

// Calling function hello
person.hello("World");

//Meera Says hello World

Solution 5 - Javascript

1.) add .jshitrc same folder level with your app.js and other files

2.) put this inside the newly created file { "esversion": 6 }

3.) never use single quote ' use backticks `

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRon IView Question on Stackoverflow
Solution 1 - JavascriptTim GrantView Answer on Stackoverflow
Solution 2 - JavascriptilyasView Answer on Stackoverflow
Solution 3 - Javascriptjama.bushraView Answer on Stackoverflow
Solution 4 - JavascriptsunilsinghView Answer on Stackoverflow
Solution 5 - JavascriptAljohn YamaroView Answer on Stackoverflow