We've done a lot with numbers, but there's more to this world than just math. Let's look at words.
> "Hello world!"; "Hello world!"
The stuff inside the quotes is called a string. Strings can include letters, punctuation, and even numbers.
> "5674"; "5674" > "!?&"; "!?&" > "Strings are crazy! ;)"; "Strings are crazy! ;)"
What happens if we don't surround our string with quotes?
> hello; x Uncaught ReferenceError: hello is not defined (...)
We can set variables equal to strings:
> const myString = "Strings can contain characters like @, $, and %."; undefined
If you want to put a quote inside a string, you have two options. Here's the first:
> "Quoth the raven, \"Nevermore.\""; "Quoth the raven, "Nevermore.""
" that comes right after it is not the end of the string, but just a character inside the string.
You can also use single quotes:
> '"Programming is fun!", she exclaimed.'; ""Programming is fun!", she exclaimed."
"Strings can contain letters, numbers like 1, 14.5, and 5784329, and characters like @, $, and %."
You can assign variables to strings:
> const myString = "Strings can contain characters like @, $, and %.";
Include quotes in strings by either escaping them with
\\, or by using single quotes outside:
"Quoth the raven, \"Nevermore.\"";
You can use either single quotes
' ' or double quotes
" " around your strings, as long as the start and end quote marks match.
'This is a string surrounded by single quotes.'; "This is a string surrounded by double quotes.";
Lesson 9 of 23
Last updated June 18, 2020