Include variables in a string in JavaScript
Given a variable and a string, include the variable in a string via interpolation, and write a program to demonstrate the same.
Solution
To include a variable or an expression in a string in JavaScript, we can use template strings. Template strings interpolate the variables or expressions in a string.
`hello ${name}. welcome to javascript tutorials.`
Program
1. Given a string in variable name
, include the variable in the template string greeting
.
let name = 'Arjun'; //normal string
let greeting = `hello ${name}. welcome to javascript tutorials.`;
console.log(greeting);