How can I do string interpolation in JavaScript?

前端 未结 19 2509
慢半拍i
慢半拍i 2020-11-21 07:54

Consider this code:

var age = 3;

console.log(\"I\'m \" + age + \" years old!\");

Are there any other ways to insert the value of a variabl

19条回答
  •  名媛妹妹
    2020-11-21 08:17

    Since ES6, you can use template literals:

    const age = 3
    console.log(`I'm ${age} years old!`)

    P.S. Note the use of backticks: ``.

提交回复
热议问题