Sub topic 1
Creating Strings: Quotes, Escapes, and Primitives
A string in JavaScript is a sequence of characters used to represent text. This chapter covers the three ways to write a string literal, how escape sequences put special characters into a string, and what it means that strings are primitive values.
Prerequisites
This chapter assumes you can:
- Declare variables with
constandletand call functions. - Run JavaScript in a browser console or with Node.js.
- Read
console.logoutput.
Three Kinds of Quotes
JavaScript accepts single quotes, double quotes, and backticks. Single and double quotes behave the same way. Backticks create template literals. Template literals can span several lines and can embed expressions. A later chapter covers them in full.
const single = 'It\'s here';
const double = "She said \"hi\"";
const mixed = "It's easier to switch quote styles";
const multi = `Line one
Line two`;
console.log(single); // It's here
console.log(double); // She said "hi"
console.log(mixed); // It's easier to switch quote styles
console.log(multi); // Line one
// Line twoPick one quote style for your project and use it consistently. Linters and formatters such as Prettier can enforce the choice. If a string contains your usual quote character, you can switch to the other style so you don't have to escape it.
Escape Sequences
A backslash starts an escape sequence. An escape sequence is a short code that stands for one character that would be hard or impossible to type directly.
| Sequence | Meaning |
|---|---|
| `\n` | Newline |
| `\t` | Tab |
| `\\` | A single backslash |
| `\'` and `\"` | A literal quote |
| `\xHH` | Character with hex code HH, such as `\x41` for A |
| `\uHHHH` | UTF-16 code unit with four hex digits, such as `\u00e9` for é |
| `\u{H...}` | Any Unicode code point, such as `\u{1F600}` for 😀 |
console.log("Tab:\tend"); // Tab: end
console.log("C:\\temp\\new"); // C:\temp\new
console.log("\x41", "\u00e9", "\u{1F600}"); // A é 😀
console.log("a\nb".length); // 3The last line prints 3 because \n is one character. It is two characters in the source code but one in the string. A common bug is writing a Windows path such as "C:\new". JavaScript reads \n in that path as a newline, so you must write the backslash twice, as "C:\\new".
Strings Are Primitives
A primitive is a value that is not an object and has no properties of its own. typeof reports "string" for a string primitive. Primitive strings are compared by their content, not by where they are stored in memory.
const a = "hello";
const b = "hel" + "lo";
console.log(typeof a); // string
console.log(a === b); // true
console.log(a.length); // 5
console.log(a.toUpperCase()); // HELLOYou can still call methods like toUpperCase on a primitive. When you do, JavaScript wraps the value in a temporary String object for the duration of the call. The chapter on primitives versus String objects explains why you should avoid new String(...).
Exercise
What does console.log('a\\nb'.length) print, and why?
Show answer
It prints 4. The sequence \\ is an escaped backslash, so the string contains a, a backslash, n, and b. It does not contain a newline.
Exercise
Does "It's" === 'It\'s' evaluate to true or false?
Show answer
It evaluates to true. Both literals produce the same three-character primitive, and primitive strings are compared by content.