Your pizza party program can already count guests and work out a bill. What it can’t do yet is tell anyone about it. A bare 24 in the terminal means little to a friend reading the group chat, but a line like Pizza night at Sam’s place! says exactly what’s going on.
To write messages like that, your program needs to work with text as a value, just like it works with numbers. In this lesson, you’ll predict what text expressions produce and how long the resulting strings are. Then you’ll build a short party message from names and pieces of text, checking each step in the Node REPL or a small script.
Write Text Between Quotes
Open the integrated terminal in your course folder and start the REPL with node. Then type a few words inside double quotes and the same words inside single quotes:
> "Pizza night"
'Pizza night'
> 'Pizza night'
'Pizza night'
A piece of text inside quotes is a string, the JavaScript value for text. Double quotes and single quotes make exactly the same string, so pick whichever you like and stick with it. The REPL shows a string with quotes around it so that you can tell it apart from a number or a name. Those quotes aren’t part of the text, which you can see when you print it:
> console.log("Pizza night")
Pizza night
undefined
The printed line has no quotes. The undefined underneath is the REPL echoing what console.log() itself gives back, not part of your text.
The quotes do one important job, though. Back in the first lesson of this course, you saw that a word without quotes is a name that JavaScript looks up. Leave the quotes off, and JavaScript goes looking for a name that doesn’t exist:
> Pizza
Uncaught ReferenceError: Pizza is not defined
So when you see a ReferenceError for a word that was meant to be text, check for missing quotes first.
Put Quotes and Newlines Inside a String
Your party message needs an apostrophe in Sam’s. Inside single quotes, that apostrophe would end the string too early and leave JavaScript confused about the rest. One fix is to choose the other kind of quotes for the outside:
> "Pizza night at Sam's place!"
"Pizza night at Sam's place!"
Notice that the REPL switched to double quotes in its echo. It picks whichever quotes don’t clash with the text, which is a good habit for you, too.
Sometimes you can’t avoid the clash, or you need a character that you can’t type straight into a string, like a linebreak. For these cases, JavaScript has escape sequences. Each one is a backslash followed by one character, and the pair stands for something special: