0

I've started Javascript 30. What bothers is like on given example code:

const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);

Why are the arguments put into the backticks? I understand how to use double and single quotes and when I can use both. However I don't understand how and when to use backticks.

Teemu
  • 22,918
  • 7
  • 53
  • 106
Luke_Nuke
  • 461
  • 2
  • 6
  • 23
  • [Template Literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) – James Aug 04 '17 at 22:12
  • Where are you using backtick¿? – Javier Jimenez Matilla Aug 04 '17 at 22:12
  • Oh, it's not displayed. I don't know why. However, it is put before audio and closed after closing square bracket ]. \`(audio[data-key="${e.keyCode}"]\`) – Luke_Nuke Aug 04 '17 at 22:15
  • It's because SO uses backticks to `do this to text`, if you put it in a code block they would show up. – James Aug 04 '17 at 22:16
  • @James I used 4 space to display this as code but it didn't work. Do you know why? So I could fix that. – Luke_Nuke Aug 04 '17 at 22:23
  • @Luke_Nuke I've already formatted the code in your question. But in general, you can select a code block (also multiple lines), and then click `{}` icon on editor tools, that'll convert the selected text to a code block. – Teemu Aug 04 '17 at 22:27
  • 1
    @Teemu thank you for both, editing and advice! :) – Luke_Nuke Aug 04 '17 at 22:30
  • this question is duplicated: https://stackoverflow.com/questions/27678052/what-is-the-usage-of-the-backtick-symbol-in-javascript – Javier Jimenez Matilla Aug 04 '17 at 22:41

3 Answers3

2

The backticks allow you to use the string interpolation syntax ${}.

SimpleJ
  • 13,812
  • 13
  • 53
  • 93
0

You can add expression in string with back-tick. This feature was add on ES6.

0

Backticks are template literals. They allow for evaluation of variables and expressions inside of a string:

var a = 5, b = 10;

document.write(`${a} + ${b} = ${a + b}`);
Ryan Tuosto
  • 1,941
  • 15
  • 23