Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion

General practice question, should I use " " or ' '?

Just a general practice question, which quotation method is the most commonly used among professional, I want to stick to one but can't make up my mind.

This is for HTML, CSS, JavaScript and PHP

3 Answers

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

In html, css and javascript it doesn't really matter which one you use.

In php single-quotes displays exactly what between them, but double-quotes gives you a bit more magic.

$firstName = 'Sunny';

echo 'Hello $firstName'; // prints Hello $firstName
echo "Hello $firstName"; // prints Hello Sunny

I hope this makes some kind of sence? :-P

Oh wow thanks a lot Henrik! this is very useful information regarding PHP

Like the PHP example above, if you ever wind up scripting anything with the bash command line (shell) on linux or OSX, the distinction between single and double quotes will matter there too. Same with a number of other languages like Ruby and Perl.

In Python, it explicitly does not matter. Quoting the PEP8 style guide for python: "In Python, single-quoted strings and double-quoted strings are the same. This PEP does not make a recommendation for this. Pick a rule and stick to it. When a string contains single or double quote characters, however, use the other one to avoid backslashes in the string. It improves readability."

Basically, it's probably prudent to look for the canonical reference for the language you have a question about, e.g. JS, PHP, etc

Jonathan Kuhl
Jonathan Kuhl
26,133 Points

Depends on the language. Most of them, you can pick whichever and it's your preference. There are a few, like PHP mentioned above, where it does matter. Be aware of that.

However, if it doesn't matter in the code, then simply be consistent. I prefer double quotes because I have to train myself to not press "shift" when I hit the apostrophe key. It's too much of a habit for me. Just be consistent. If you use double quotes for one string, use it for all of your strings, unless their JavaScript template strings. Then use backticks. Because template strings are awesome.