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

JavaScript JavaScript Basics (Retired) Storing and Tracking Information with Variables Strings and Numbers

Ro Man
Ro Man
206 Points

Is there a best practise approach to "double" and 'single' quotes? I mean, is there one solution to be preferred?

Hi. I'm just starting out and I'm not sure, how to choose either single or double quotes when writing code.

Thanks for the feedback.

5 Answers

There really isn't a single "best practice" when it comes to using single or double quotes, as you'll use whatever works the best with the content being wrapped in them. I'll use single quotes when I am wrapping html in a string as it it allows me to use double quotes for attributes without having to string escape. Double quotes are nice when you have a string with lots of apostrophes.

var html = '<div class="cool-div"><a href="#">Link</a></div>';

var string = "There's some good reasons to use double quotes, it's usually what I do for paragraph text.";

According to JSON standards, JSON strings must be double quoted. Aside from that, its like Luke said; you'd typically choose one to avoid quote escaping.

Ro Man
Ro Man
206 Points

Great. Thanks for the help. Will make it easier for me to choose what to use.

Saqib Ali
Saqib Ali
3,686 Points

Hi guys just to add on to the best practise question, is there a preference when using JS within a CMS, such as WordPress?

Good question, I would argue that the practices would still remain the same. The WP coding standards for example say to use single quotes with string literals: https://make.wordpress.org/core/handbook/best-practices/coding-standards/javascript/. It may vary though depending on who's coding standards you are trying to follow and if they have their own opinion of how to handle quotes.

Saqib Ali
Saqib Ali
3,686 Points

Cheers Luke, appreciate the info!