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

Using quote marks in javascript

Hi all,

I was wondering when exactly are you supposed to use quote marks and does it matter if it is single or double? Does it have to be the same single/double consistency in each statement? I know that you don't use them around variable names or when listing a variable.. but that's about it. Thanks!

1 Answer

Steven Parker
Steven Parker
243,266 Points

Quote marks are used to create a string literal, when you want a word or phrase to be used as itself and not represent something else (as a variable would).

JavaScript supports 3 kinds of marks for quoting, the "double" or standard quote ("), the apostrophe or "single" quote ('), and the accent or "back-tick" (`). They can be used interchangeably, even on the same line, as long as they are always in matched pairs. However, the third type is usually only used to create a template literal for interpolation (substitution of expressions or symbolic references).

Some examples:

document.write("Here is a normal string literal.<br>");
document.write('Single quotes are good when double quote(s) are inside... " &larr;see?<br>');
document.write(`Template literals can have expressions inside: A week has ${24*7} hours.`);