In JavaScript, a backtick† seems to work the same as a single quote. For instance, I can use a backtick to define a string like this:
var s
Backticks (`
) are used to define template literals. Template literals are a new feature in ECMAScript 6 to make working with strings easier.
Features:
Note: we can easily use single quotes ('
) and double quotes ("
) inside the backticks (`
).
Example:
var nameStr = `I'm "Rohit" Jindal`;
To interpolate the variables or expression we can use the ${expression}
notation for that.
var name = 'Rohit Jindal';
var text = `My name is ${name}`;
console.log(text); // My name is Rohit Jindal
Multi-line strings means that you no longer have to use \n
for new lines anymore.
Example:
const name = 'Rohit';
console.log(`Hello ${name}!
How are you?`);
Output:
Hello Rohit!
How are you?