xxxxxxxxxx
//Template strings allow you to inject values into a string while
//preserving the format. Your code becomes more reader friendly.
//Use it instead of 'string arithmetic'.
let userName = 'John Doe';
let finalResult = 234;
//template string
console.log(`Hello ${userName} your final result is ${finalResult}`);
//'string arithmetic'
console.log("Hello " + userName + " your final result is " + finalResult);
xxxxxxxxxx
var name = 'World';
var cname = 'javaTpoint';
console.log(`Hello, ${name}!
Welcome to ${cname}`);
xxxxxxxxxx
const classes = `header ${ isLargeScreen() ? '' :
`icon-${item.isCollapsed ? 'expander' : 'collapser'}` }`;
xxxxxxxxxx
const name = 'JavaScript';
console.log(`I love ${name}`); // I Love JavaScript
xxxxxxxxxx
/*
The template string is indicated with a dollar sign and curly brackets
(${expression}) inside the backtick symbol.
*/
//Template String
const name = 'Muhammad Shahnewaz';
const result = `${name} is a good boy`;
console.log(result); //Muhammad Shahnewaz is a good boy
xxxxxxxxxx
function hello(firstName, lastName) {
return `Good morning ${firstName} ${lastName}!
How are you?`
}
console.log(hello('Ranjeet', 'Andani'))
xxxxxxxxxx
const name = 'John Doe';
const age = 30;
const occupation = 'developer';
const message = `Hello, my name is ${name}. I'm ${age} years old and I work as a ${occupation}.`;
console.log(message);