How can I do string interpolation in JavaScript?

前端 未结 19 2542
慢半拍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:25

    You can do easily using ES6 template string and transpile to ES5 using any available transpilar like babel.

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

    http://www.es6fiddle.net/im3c3euc/

提交回复
热议问题