Template literal texts in JavaScript

Sonamojha
1 min readMay 17, 2024

Template literals are a feature in JavaScript that make it easier to work with strings, we use backticks (`) instead of single or double quotes.

let’s understand with an example

const name = 'John';
console.log('Hello, ' + name + '!');

//in this we are using single quotes that is making complicate to concate it

//in this case we canuse backticks (\`) to create template literals like this:

const name = 'John';
console.log(`Hello, ${name}!`);

The `${}` syntax inside the backticks allows us to embed variables directly into the string. and this is very useful for multiple variables.

const a = 10;
const b = 5;
console.log(`The sum of ${a} and ${b} is ${a + b}.`);

Template literals also support multi-line strings without needing to use escape characters or concatenation:

const multilineString = `
This is a
multi-line
string.
`;
console.log(multilineString);


//It will give exact output
This is a
multi-line
string.

So, template literals are a handy way to work with strings in JavaScript, especially when we need to include variables or create multi-line strings.

--

--