How can I do string interpolation in JavaScript?

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

    You could use Prototype's template system if you really feel like using a sledgehammer to crack a nut:

    var template = new Template("I'm #{age} years old!");
    alert(template.evaluate({age: 21}));
    

提交回复
热议问题