When should you use single quotes or double quotes for strings?

  1. When to use single quotes (’ ') and when double (" ")?
  2. Does numbers include negative?
  1. This is a personal preference, I prefer the high visibility of double quotes myself. Also this is one thing that ES6 Template Literals help solve (its a better string!)

  2. Yes

12 Likes

Why are single quote strings used to frequently here? While in the case of JavaScript, string literals do not exist in their proper context, it severely damages readability of the script when you get into value substitution and use of escape characters while still encasing them with single quotes, which in a majority of languages, albeit mainly compiled ones, represents a literal string. A string with no variables, no escape characters, and an innate form of simple input validation.

1 Like

i dont know? but can someone please tell me

I personally think strings should be with double quotation marks even just for avoiding this simple error:
console.log('I don't like double quotation');

17 Likes

As per Airbnb Best Practices:-
We should use single quote strings as it is a Good Practice. You can still use double quotes but not a Good Practice.

But is there any good reason why?

If you want to print something covered with double quote you should use single quote to the console.

console.log ( ' I love "codecademy" ' )

Or if you want to print something covered with single quote you should use double quote to the console.

console.log ( " I Love 'codecademy' ")

Many other reasons may be there. I have found this at the beginning level of my learning js.

35 Likes

Personally, I prefer using single quote for strings in cases of necessary double quotes needed:

//A case without double quote in the string.
Ex1: console.log(‘I don’t know why I have to use single quote’);

//A case of double quote in the string.
Ex2: console.log(‘Now I know why I have to use the “double quote” for strings’);

6 Likes

What if I wanted to print the phrase: It’s a “moot” point?

Is this not possible since it has both single and double quotes, or is there some other way to do it?

Thanks!

When you are dealing with strings.
I do recommend use single quotes by default.
console.log(' Hello World! From Jorcus ');

If you are dealing with multiple lines of strings and included variables over there.
Then use template literals

`string text line 1
 string text line 2
Hello from ${name}!`
1 Like

Hello, @joepassafiume3223358, and welcome to the forums.

You have a couple of options for your specified output. You can use the escape character \ for the quotes that need to be printed, and are the same as the outer quotes:

console.log("It's a \"moot\" point.");
//Or
console.log('It\'s a "moot" point.');

Or, as @webninja37932 pointed out, you can use the back tick to enclose both single and double quotes:

console.log(`It's a "moot" point.`);

All three will produce the same output.

6 Likes

In cases where you want to to use normal strings use single quotes like:
‘I love coding.’

Use double quotes where you want to use single quotes or apostrophe in strings like:
“It’s been my experience that you can nearly always enjoy things if you make up your mind firmly that you will.”
"I like the quote ‘Happiness is a state of activity.’ "

Numbers can be negative, positive or zero:
-10, 0, 5

Thanks for your reason. I think I’ll try it out and see if I like to do that, too.

I see the phrase “best practice” and I wonder exactly why it is considered “best practice”.

Can anyone explain just what drives that label?

In my thinking, double quotes makes far more sense than a single quote, that is to say, an apostrophe, which is going to be used far more often within a string that a double quote.

Further… a double quote is more easily seen, standing out on the screen. A single quote/apostrophe can look too much like a speck of dust.

1 Like

The most telling statement is this run the code console.log('abc' === "abc"); It will come back as true.

As far as I have been able to glean there is no difference and as has been said before they are interchangeable.

By “Best Practice” in programming and other tech circles it means is " A best practice is a technique or methodology that, through experience and research, has proven to reliably lead to a desired result. … Throughout the software industry, several best practices are widely followed." That is from WhatIs.com and I think that it sums it up pretty well.

So “best practice” in this sense is what most people(i.e. industry professionals) see as the best reasoning.

My take on it and from what I have been told either is fine but be consistent. If you are using double quotes then use them throughout your entire code do not start switching between them for no reason. So if you are going to print something to console and want to use double quotes to quote something inside a string you will need to use the \ in front of the double quotes to make them work and not use single quotes just to make it easier. For example, don’t do this just for convenience console.log('I love to "quote" things'); if you are using double quotes in the rest of your code. You should be consistent and use either console.log("I love to \"quote\" things"); or console.log("I love to 'quote' things");

To clarify(or confuse things) further, at your job best practices may differ. Follow the guidelines of whoever you are writing the code for.

Personally I use single quotes. I like the look and like was stated before use template literals, they are the bee’s knees. Consistency is key and makes for readable code. Like any language be consistent.

Either way, hope that helps a little bit