Unveiling the Magic of Backticks, Single Quotes, and Double Quotes in JavaScript

Sathira Nipun Wijerathna
3 min readJul 6, 2023

--

When working with strings in JavaScript, you have the flexibility to choose from three quoting styles: double quotes ( "" ), single quotes ( '' ), and the newer backticks/template literals ( `` ). Each style has its unique characteristics and uses.

In this article, we will explore these styles in detail, starting with double quotes, followed by single quotes, and finally, the backtick style.

Let’s dive in!!!

Double Quotes

Double quotes ("") are a widely used quoting style in JavaScript. They are straightforward and easy to understand.

//Code
const greeting = "Hi, welcome to the JS Training";
console.log(greeting);

//Output
Hi, welcome to the JS Training

Double quotes are useful when you need to include single quotes within your string without explicitly escaping them.

//Code
const greeting = "Hi, 'Good Morning'";
console.log(greeting);

//Output
Hi, 'Good Morning'

However, one drawback of double quotes is that you will need to escape any double quotes used within the string itself.

You have to use the single quote style or backslash to escape any double quotes inside double quotes.

Single Quotes

Single quotes ('') are also commonly used in JavaScript for defining string literals. They are similar to double quotes and can be used interchangeably.

//Code
const name = 'Sathira';
console.log('Hi, ' + name + ' is here!');

//Output
Hi, Sathira is here!

Like double quotes, single quotes allow you to include double quotes within the string without escaping them explicitly.

//Code
const greeting = 'Hi, "Good Morning"';
console.log(greeting);

//Output
Hi, "Good Morning"

You have to use the double quote style or backslash to escape any single quotes inside single quotes.

Backtick Style (Template Literals)

The backtick style, also known as template literals, was introduced in ES6. It offers advanced string formatting capabilities and supports multiline strings. Backticks (``) are used to enclose template literals.

//Code
const name = 'Sathira Nipun';
console.log(`Hi, ${name}!`);

//Output
Hi, Sathira Nipun!

Template literals allow you to embed expressions using ${expression} syntax, making string concatenation more readable and convenient. They also facilitate multiline strings.

//Code
const multiLineString = `
Hi,
JS Beginners!!!
This is a
multiline
string.
`;
console.log(multiLineString);

//Output
Hi,
JS Beginners!!!
This is a
multiline
string.

Understanding the different string quoting styles in JavaScript empowers you to choose the most appropriate one for your coding needs. Double quotes and single quotes are traditional styles that are widely used and offer their own advantages.

However, the backtick style, with its template literals, provides additional flexibility and readability.

Consider the specific requirements of your project and select the quoting style that suits it best.

Happy coding!!!

--

--