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 trialmohammed saleh
375 Pointswhat's the difference between a double quote and a single quote?
Strings and numbers.
3 Answers
adefee
4,705 PointsIntegers, decimals etc shouldn't be quoted, except in special cases (such as the number being too large for an integer field in a database). Strings use single/double quotes.
Often, single vs double quotes is a matter of preference - though some languages may treat them differently in small (but very impactful) ways. Specific to your question, JS has no such differences and the choice is entirely up to the developer. PHP, on the other hand, treats a single-quoted string literally (more or less as plaintext), but will do basic evaluation on double-quotes strings (like interpreting line-breaks ("\n")).
A few tidbits you may find useful:
- If you know your string is going to contain a single quote, then you may want to encase it in double quotes (or vice versa).
- In JS, most code you look at by experienced devs will be probably single quotes because it's less effort to type (fewer keystrokes: double quotes requires you to hold shift) - unless double quotes were chosen for consistency (in my experience, tends to be the main reason you see double-quotes en masse).
- JSON (super common, but with which you may or may not be familiar yet), when properly formatted, ALWAYS uses double quotes around string values. There's no user preference in such cases.
Saqib Ishfaq
13,912 PointsOn number values, we don't use quotes, only strings needs to be quoted with " " or ' ' It only a matter of preference really ! Thers no difference between them ... also depending on the situation we can use both as well in some values ! Like for example if in a string there's another value which needs to be quoted then we can use " " on the outer and ' ' on the inner value !
Melvin Ray
Courses Plus Student 6,312 PointsHi there, Mohammed!
In JavaScript there is no difference between double - and single-quotes as long as you match double to double and single to single at the ends of a string.
The only time it will matter whether to use single- or double-quotes is when one or the other is specified in the coding guidelines for a particular project you are working on so that consistency of the code is maintained. That way, everyone will be on the same page. (Pun intended!)