Home > Blockchain >  How do i output a javascript template literal to the console
How do i output a javascript template literal to the console

Time:10-16

var variable = 2;

var x = `${variable}`;
console.log(x) to output `${variable}`

not 2 thank you. am new to programming

CodePudding user response:

Welcome to Programming. Depending on the overall goal here, I would maybe set another variable, say y to the string of the template literal.

var y = '`${variable}`'

Unfortunately, Javascript will always evaluate the value stored in a variable storing a primitive data type like a string or number.

CodePudding user response:

You can escape the backticks:

var variable = 2;

var x = `\`${variable}\``;
console.log(x)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related