94

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}
1
  • 20
    Use grave-accents ` and not single-quotes ' or double-quotes ". Commented May 16, 2016 at 2:04

2 Answers 2

211
+50

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

Sign up to request clarification or add additional context in comments.

3 Comments

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.
"If you're using a QWERTY keyboard" ...with a US layout. Some QWERTY layouts (e.g. QWERTY JIS) do not place the backtick there.
3

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

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.