How can I do string interpolation in JavaScript?

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

    If you want to interpolate in console.log output, then just

    console.log("Eruption 1: %s", eruption1);
                             ^^
    

    Here, %s is what is called a "format specifier". console.log has this sort of interpolation support built-in.

提交回复
热议问题