querySelector method arguments are put into backtick, why?

≯℡__Kan透↙ 提交于 2021-02-04 14:19:11

问题


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.


回答1:


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




回答2:


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




回答3:


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


来源:https://stackoverflow.com/questions/45516152/queryselector-method-arguments-are-put-into-backtick-why

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!