Essential JavaScript
Introduction
JavaScript Basics
Numbers
Strings
Logic
Variables
Loops
Objects
Arrays
Going Further
Essential JavaScript
Introduction
JavaScript Basics
Numbers
Strings
Logic
Variables
Loops
Objects
Arrays
Going Further
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
I can create a string with quotation marks in it
Code Editor
Click "Run Code" to execute your JavaScript in a secure sandbox and see the output below.
Console Output
0 output lines