JavaScript Code

JavaScript Template Strings


Template Strings

unlike normal strings, template strings are enclosed in back-ticks ``

let message = `hello world`;

Single and Double Quotes

with template strings, we can use single and double quotes inside a string without escaping

let message = `hello "world"`;
let message = `hello 'world'`;

Multiline Strings

with template strings we can define multiline stings without using a new line escape character \n inside the string

let message = `hello world
welcome to new world
learn javascript here`;

Variables inside String

template strings allow interpolation of variables or expressions inside a string

the syntax to interpolate a variable inside a template string is

${variable}

the following is an example to include a variable in a template string

let name = 'Arjun';
console.log(`Hello ${name}`);
let a = 4;
let b = 8;
console.log(`${a} + ${b} = ${a + b}`);