What a string is
A string is a piece of text: any sequence of characters you can type, including letters, digits, spaces and symbols. Programs use strings for names, messages and anything else made of text. In TypeScript the type of a string is string.
You create a string by putting text inside quotes. Single quotes and double quotes work the same way:
console.log('My name is Andy');
console.log("Welcome!");
My name is Andy
Welcome!
The quotes are not part of the string. Their job is to tell the compiler where the text starts and where it ends, so they are not printed.
Single or double quotes
Pick one style and use it consistently. The choice matters only when the text itself contains a quote. If the text has an apostrophe, wrap it in double quotes, and if it has double quotes, wrap it in single quotes:
console.log("It's fine");
console.log('She said "hi"');
It's fine
She said "hi"
You can also keep your usual quotes and put a backslash before the quote inside the text, which tells TypeScript the quote belongs to the string. 'It\'s fine' prints the same as the first line above. The backslash is an escape character: \n starts a new line, and \\ prints one backslash.
console.log("line1\nline2");
line1
line2
Strings versus numbers
Quotes change what a value is. 1998 is a number and "1998" is text that happens to contain digits. They print identically, but they behave differently:
console.log(1998 + 2);
console.log("1998" + 2);
console.log(typeof "5", typeof 5);
2000
19982
string number
With a string on either side, + joins text instead of adding. Joining is called concatenation. For example, "Hello" + " " + "world" gives Hello world.
A few things strings can do
Every string knows its own length and can be asked for single characters or for changed copies of itself:
console.log("Hello".length, "".length, "a b".length);
console.log("abc"[0], "abc".toUpperCase());
5 0 3
a ABC
A space counts as a character, and an empty string "" is a valid string with length 0. Strings cannot be changed in place. "abc".toUpperCase() returns a new string, and writing something like s[0] = "b" on a string variable is rejected by the compiler with an error saying the index signature only permits reading.
Backticks
TypeScript has a third kind of quote, the backtick. A string in backticks is a template literal, and it can contain expressions inside ${}, which are replaced by their values:
console.log(`2 + 3 = ${2 + 3}`);
2 + 3 = 5
For plain text, single or double quotes are enough. Backticks are worth reaching for when a message mixes text and values.
Common mistakes
- A missing closing quote.
console.log("Welcome!);never ends its string, so the compiler reports an unterminated string literal. Every opening quote needs a matching closing one of the same kind. - Mixing quote kinds.
'Hey there!"starts with a single quote and ends with a double quote, so the string never closes. - Forgetting the quotes.
console.log(Hello)looks for a variable namedHello. Without quotes, text is treated as a name. - Quoting a number by accident.
"31"prints like 31 but cannot be used like a number without being converted.
The exercise
The exercise asks you to print the text I love coding!:
- Write
console.log(to start the call. - Type the text inside a pair of double quotes, exactly as given, including the capital letter, the space between words and the exclamation mark.
- Close the parenthesis and add a semicolon.
The finished line is console.log("I love coding!");. The lesson's tests compare the output with the expected text, so a different capital letter or a missing space will fail.
Key takeaways
- A string is text, written between single or double quotes.
- The quotes are not part of the string and are not printed.
- A quote kind inside the text needs the other kind around it, or a backslash in front of it.
1998is a number,"1998"is a string, and+treats them differently.