JavaScript equivalent of Python's format() function?

前端 未结 17 2121
慢半拍i
慢半拍i 2020-12-13 08:42

Python has this beautiful function to turn this:

bar1 = \'foobar\'
bar2 = \'jumped\'
bar3 = \'dog\'

foo = \'The lazy \' + bar3 + \' \' + bar2 \' over the \'         


        
17条回答
  •  星月不相逢
    2020-12-13 09:05

    There is a way, but not exactly using format.

    var name = "John";
    var age = 19;
    var message = `My name is ${name} and I am ${age} years old`;
    console.log(message);

    jsfiddle - link

提交回复
热议问题