How to Escape Backtick-Quoted Strings in JavaScript (Example!)

So we all know that there are 3 ways to present a string.

  • Single-quotes
  • Double-quotes
  • Backticks

The truth is that single and double-quotes are identical. You can use them interchangeably.

However, backticks allow you the added option to escape the string.

Backticks-quoted strings are also known as template literals.

Unfortunately, in order to make JavaScript evaluate something within a string, you’ll also need to use ${} inside the template literal. Whatever you place inside the squiggly brackets ends up being calculated, computed, or converted as directed.

How backtick-quoted strings work: Template literals for escaping the string

Example 1: Backticks strings that compute, and convert

console.log(`half of 10 is ${10/2}`)

The output is this:

half of 10 is 5

Do you see how using backticks allows us to use the ${} feature as well? This ${} can incorporate itself into any backtick service.

You see here that the ${} and everything placed within this is actually evaluated within a string that’s surrounded by backticks.

Leave a Comment