How can I do string interpolation in JavaScript?

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

    Try sprintf library (a complete open source JavaScript sprintf implementation). For example:

    vsprintf('The first 4 letters of the english alphabet are: %s, %s, %s and %s', ['a', 'b', 'c', 'd']);
    

    vsprintf takes an array of arguments and returns a formatted string.

提交回复
热议问题