Essential JavaScript

Using Quotes in a String

When we create a string with quotation marks, the quotation marks are not part of the string. They are just characters we use to tell JavaScript where the string starts and ends.

If we want to create a string with quotation marks inside it, we could run into problems. Try running this code in the console:

"a "healthy" dessert"

We will get an error because JavaScript sees the first two quotation marks and thinks the second one ends the string. Then it tries to read the rest of the characters as JavaScript code, but of course they are not valid code. That's why we get an error.

Using quotation marks in strings

One way to use quotation marks in strings is to use a different kind of quotation mark around the string. Try these examples:

'a "healthy" dessert'
"a 'healthy' dessert"

We can use any quotation mark in a string as long as it is not the same quotation mark used to define the string.

Learning Goals

Code Editor