How can I do string interpolation in JavaScript?

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

    I can show you with an example:

    function fullName(first, last) {
      let fullName = first + " " + last;
      return fullName;
    }
    
    function fullNameStringInterpolation(first, last) {
      let fullName = `${first} ${last}`;
      return fullName;
    }
    
    console.log('Old School: ' + fullName('Carlos', 'Gutierrez'));
    
    console.log('New School: ' + fullNameStringInterpolation('Carlos', 'Gutierrez'));

提交回复
热议问题