Search code examples
javascriptjquerystringvar

How to insert multiple variables into a string?


Im really new to JavaScript and I was wondering how would I be able to insert multiple varibles into a string?

var adjective1 = 'amazing';
var adjective2 = 'fun';
var adjective3 = 'entertaining';



var madLib = "The Intro to JavaScript course is (adjective1). James and Julia are so (adjective2). I cannot wait to work through the rest of this (adjective3) content!";
console.log(madLib);

Solution

  • You can use Template literals (Template strings)

    Template literals are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them. They were called "template strings" in prior editions of the ES2015 specification.

    var adjective1 = 'amazing';
    var adjective2 = 'fun';
    var adjective3 = 'entertaining';
    
    var madLib = `The Intro to JavaScript course is ${adjective1}. James and Julia are so ${adjective2}. I cannot wait to work through the rest of this ${adjective3} content!`;
    console.log(madLib);