Usage of the backtick character (`) in JavaScript

后端 未结 9 2277
囚心锁ツ
囚心锁ツ 2020-11-22 00:51

In JavaScript, a backtick seems to work the same as a single quote. For instance, I can use a backtick to define a string like this:

var s         


        
9条回答
  •  不知归路
    2020-11-22 01:35

    It's a pretty useful functionality, for example here is a Node.js code snippet to test the set up of a 3 second timing function.

    const waitTime = 3000;
    console.log(`setting a ${waitTime/1000} second delay`);
    

    Explanation

    1. Declare wait time as 3000
    2. Using the backtick you can embed the result of the calculation of 'wait time' divided by 1000 in the same line with your chosen text.
    3. Further calling a timer function using the 'waitTime' constant will result in a 3 second delay, as calculated in the console.log argument.

提交回复
热议问题