How can I do string interpolation in JavaScript?

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

    Try kiwi, a light-weight JavaScript module for string interpolation.

    You can do

    Kiwi.compose("I'm % years old!", [age]);
    

    or

    Kiwi.compose("I'm %{age} years old!", {"age" : age});
    

提交回复
热议问题