88

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}
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
Ron I
  • 4,090
  • 8
  • 33
  • 64

8 Answers8

206

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

Tim Grant
  • 3,300
  • 4
  • 23
  • 31
  • 9
    Wow, you wouldn't believe how long it took me to find this. It is incredibly not evident this was the problem, especially sense the back tick is used to CREATE code segments in Markdown and the like. It's really easy to just think the back tick was a code marker and then mentally translate it to a single tick. Thank you, thank you kindly. – Andrew T Finnell Nov 17 '16 at 17:14
  • 1
    "If you're using a QWERTY keyboard" ...with a US layout. Some QWERTY layouts (e.g. QWERTY JIS) do not place the backtick there. – Amadan May 20 '22 at 13:15
6

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})

Lloyd Dominic
  • 778
  • 8
  • 25
ilyas
  • 61
  • 1
  • 3
  • Be careful, there are many different keyboard layouts! The British QWERTY layout has `"` as `shift + 2` with both `'` and `\`` having their own keys where no modifier is needed. – phuzi May 20 '22 at 13:14
3

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

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

blackgreen
  • 34,072
  • 23
  • 111
  • 129
0

// 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
adiga
  • 34,372
  • 9
  • 61
  • 83
sunilsingh
  • 503
  • 1
  • 5
  • 9
0

I was not able to get the the desired output. I was using single quotes ' which was incorrect and it was printing the same message.

Backticks is below ~ in your keyboard. Use shift+~ to get backticks

Hope it helps.

Akshay
  • 151
  • 1
  • 3
0

Template Literal Dont use Double/Single Quote Instead Use Backtick

const test = 'Test'
console.log(`test: ${test}`)

Go to Mdn doc for more https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

Edwald
  • 1
  • 1
0

You are using single qoute not template literals Use backticks console.log(categoryName: ${this.categoryName}\ncategoryElements: ${this.categoryElements} );

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34576647) – AztecCodes Jun 26 '23 at 11:42
-4

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 `

Aljohn Yamaro
  • 2,629
  • 25
  • 22