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 trialmonique champagne
761 Pointssingle or double quotes!
I know this is probably very obvious but what are all the string literals like snow, rain, and sun in single quotes but the string literals that are in the console.log in double quotes. i thought it was up to the programmer to decide which one they wanted to use, but if i change 'sun' to "sun" i get an error..
const weather = 'snow';
if ( weather === 'sun' ) { console.log( "I'm going swimming." ); } else if ( weather === 'rain' ) { console.log( "It's raining, so I will read a book." ); } else if ( weather === 'snow' ) { console.log( "It's snowing, so I'm going sledding!" ); } else { console.log("I don't know what I'm doing today."); }
3 Answers
Akiel Adams
Full Stack JavaScript Techdegree Graduate 27,411 PointsHey Monique!
Could you share what error you're getting from using double quotes with "sun". Just curious as there shouldn't be any error as far as I know.
As for the "console.log" statements needing to use double quotes for their string literals. It all comes down to the fact that the strings contain apostrophes in words like "I'm", and "It's". Due to the fact that the apostrophe and the single quote share the same key, you must use double quotes otherwise the javascript engine will believe that the string ends at the apostrophe. This will likely causing errors as it tries to figure out what everything else means.
What happens when using 'single quotes':
console.log('I do not know if I'm going to the supermarket today')
// In the example above, the javascript engine only recognizes:
// 'I do not know if I' as the entire string. (Highlighted in orange)
Additionally, since the string is concluded at the single quote in the word "I'm", the single quote at the end, after the word today, marks the beginning of a new string literal that doesn't have an end. This is something that will likely cause javascript to throw an error.
What happens when using "double quotes":
console.log("I do not know if I'm going to the supermarket today.")
// In this example above, the javascript engine recognizes:
// "I do not know if I'm going to the supermarket today" as the entire string (Highlighted in orange)
Same thing can happen when trying to use multiple double quotes
console.log("I would like to "Jimmy" to step up to the plate.")
// In the example above, the javascript engine recognizes:
// "I would like to " and " to step up to the plate." as two separate strings. (Highlighted in orange)
In this example the word "Jimmy" isn't included in the string and will likely throw up an error as it's unexpected in the syntax.
What happens when using 'single quotes'
console.log('I would like "Jimmy" to step up to the plate')
// In this example above, the javascript engine recognizes:
// 'I would like "Jimmy" to step up to the plate' as the entire string (Highlighted in orange)
Doing it this way would yield the intended result.
Using an escape character
If you decided you really want to use all single quote for a string that contains apostrophes, or double quotes within a string literal created with double quotes. You do have the option of using an escape character before the the respective quote mark.
console.log('I do not know if I\'m going to the supermarket today.')
// javascript will recognize this as:
// 'I do not know if I'm going to the supermarket today' not displaying showing the backslash.
console.log("I would like to \"Jimmy\" to step up to the plate.")
// javascript will recognize this as:
// "I would like to "Jimmy" to step up to the plate." not displaying showing the backslashes.
Adding the backslash () allows the ' or the " to be used within the string. However, without ending the string literal.
For more information check out this great article on strings from w3schools!
Hopefully this answers your question!
monique champagne
761 PointsThanks so much, this is super helpful. And I cant seem to figure out how to get back to that exercise to find the error.. Have a hard time revisiting things I need to watch again. Not sure how to do it!!
monique champagne
761 PointsI wish there were like a tree or a list of all the things Ive watched before that I could easily go back and forth with bc some things i retain and others i totally dont ha.