Should you use Single or Double quotes for Strings in TS

avatar
Borislav Hadzhiev

Last updated: Feb 27, 2024
3 min

banner

# Should you use Single or Double quotes for Strings in TS

There isn't an official standard for whether you should use single or double quotes for TypeScript strings.

Most projects I've worked on have used single quotes unless having to escape a single quote character in the string, in which case double quotes are used.

index.ts
// ๐Ÿ‘‡๏ธ default const str1 = 'single quotes'; // ๐Ÿ‘‡๏ธ when the string contains a single quote const str2 = "It's him"; const person = 'Bobby Hadz'; // ๐Ÿ‘‡๏ธ backticks when interpolating variables const str3 = `hello ${person}`;

single vs double quotes for strings in ts

The code for this article is available on GitHub

The example demonstrates when I would use single and double quotes in a TypeScript string.

The default for most codebases I've worked on is to stick with single quotes unless the string contains a single quote.

# Alternating quotes is better than escaping a quote with a backslash

If the string contains a single quote, it's much easier to wrap it in double quotes than to escape the quote using a backslash character /.

index.ts
const str1 = "It's him"; const str2 = 'It\'s him'; const str3 = `It's him` // ๐Ÿ‘ˆ๏ธ can also use backticks

The first string is much easier to read.

The third example uses backticks ``. But I mostly use backticks when I'm interpolating a variable in a string or when I have a multiline string.

# The TypeScript contributor guidelines

It should be noted that some people link to the TypeScript contributor guidelines, which use double quotes for strings.

This is what they use in TypeScript's codebase, not what they recommend you use in your project.

This is their preference because some of the other languages their team uses require double quotes for strings, and most programming languages allow double quotes for strings.

So, it's easier for them to be consistent and not ask people to switch between single and double quotes depending on the programming language.

# Why I prefer using single quotes

The main reason I prefer single quotes is - I don't have to use the shift key every time I declare a string.

Since most strings don't contain a single quote, I don't often have to use double quotes.

For multiline strings, I use backticks.

index.ts
// ๐Ÿ‘‡๏ธ (better) const longString = ` roses are red, violets are blue `; // ๐Ÿ‘‡๏ธ (hard to read) const longString2 = 'roses are red\nviolets are blue';

why i prefer single quotes

The code for this article is available on GitHub

The first example uses a template literal and is much easier to read than the second.

Some people prefer using double quotes for strings because JSON uses exclusively double quotes.

I don't find myself writing JSON manually as often as I find myself declaring strings, so that doesn't apply to me.

When I see a string wrapped in single quotes, I know that the string doesn't contain any escape sequences or interpolated variables.

I also find it much easier to look at empty strings that were declared using single quotes.

index.ts
const str1 = ''; const str2 = "";

Maybe I've gotten used to seeing single quoted strings over the years, but the second string from the example looks busy and unnecessary.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev