Sub topic 5
Cooked and Raw Strings
This chapter assumes you know how a tag function receives its strings array and its substitution values. It also assumes you know common escape sequences such as \n, \t, and \u0041.
So far, strings has looked like a plain array of text. Each entry actually comes in two forms. The cooked form has escape sequences processed, the same way a normal string literal does. The raw form keeps the characters exactly as they appear in your source code. You read cooked parts with strings[i] and raw parts with strings.raw[i].
Two Views of the Same Source Text
When JavaScript parses a template literal, it keeps both versions of every static part. An untagged template only ever uses the cooked version. A tag function can see both.
Seeing the Difference
This tag returns both forms of the first string part so you can compare them:
function show(strings) {
return { cooked: strings[0], raw: strings.raw[0] };
}
const result = show`a\nb`;
console.log(result.cooked.length); // 3 ('a', newline, 'b')
console.log(result.raw.length); // 4 ('a', '\', 'n', 'b')
console.log(result.raw); // a\nb
console.log(JSON.stringify(result.cooked)); // "a\nb" (JSON shows the newline as \n)The cooked string holds one real newline character. The raw string holds a backslash followed by the letter n.
How Common Escapes Compare
| Source text | Cooked value | Raw value |
|---|---|---|
| `\n` | one newline character | backslash, `n` |
| `\t` | one tab character | backslash, `t` |
| `\u0041` | `A` | backslash, `u0041` (6 chars) |
| `` \` `` | a backtick | backslash, backtick |
| `\\` | one backslash | two backslashes |
| backslash at end of line | nothing (line continuation) | backslash plus the newline |
The raw form does not undo everything. In both forms, a Windows line ending (CR LF) inside the literal becomes a single LF. Raw keeps your escape syntax, but it does not keep the source file's line-ending bytes.
The raw Property Is Parallel to strings
strings.raw is a separate array with the same length as strings, and the indexes line up. Substitutions are never part of either array, so raw processing only touches the static text:
function compare(strings, ...values) {
return strings.map((cooked, i) => ({
cooked,
raw: strings.raw[i],
}));
}
const tab = '\t'; // a real tab, created outside the template
console.log(compare`col1\t${tab}col2`);
// [ { cooked: 'col1\t', raw: 'col1\\t' }, { cooked: 'col2', raw: 'col2' } ]The value tab arrives in values exactly as it was evaluated. The raw form only changes how backslashes in the static text are read. It cannot turn an existing tab character back into \t.
Choosing Which Form to Use
- Use cooked for user-facing text, HTML, and most formatting tags. Authors expect
\nto mean a newline. - Use raw when your tag passes text to another parser that does its own escape handling, so JavaScript should not process the escapes first.
- Don't mix the two inside one tag unless you document it. Readers of your call sites can't tell which form your tag uses.
The next chapter covers String.raw, a built-in tag that joins the raw parts for you. A later chapter covers escapes that are invalid in cooked form.
Exercise
Given function t(s) { return [s[0].length, s.raw[0].length]; }, what does t\u0041\\ return, and why?
Show answer
It returns [2, 8]. The cooked form turns \u0041 into "A" and \\ into one backslash, so it has 2 characters. The raw form keeps all 8 source characters: \, u, 0, 0, 4, 1, \, \.
Exercise
A tag reads strings.raw. It is called as tagx${'\n'}y. Does the newline in the output appear as a real newline or as backslash-n?
Show answer
It appears as a real newline. The '\n' is a substitution value: a normal string literal evaluated before the call. Raw processing only applies to the template's static parts, which here are 'x' and 'y'.