Sub topic 3
Inside the Call: The Strings Array and the Values
This chapter assumes you know JavaScript rest parameters (...values) and template literal syntax with ${}. The previous chapter showed that writing a tag in front of a template literal turns it into a function call. Here we look at the two kinds of argument that call receives and the rules that decide their shape.
Two Kinds of Argument
JavaScript splits a tagged template at every ${...} boundary. The text between substitutions becomes the static string parts. The expressions inside ${} become the values.
The tag receives the string parts as one array in its first argument. It then receives each value as a separate argument, so a rest parameter is the natural way to collect them.
function inspect(strings, ...values) {
console.log(strings); // [ 'Hello ', ', you are ', ' years old.' ]
console.log(values); // [ 'Ada', 36 ]
return strings.length - values.length; // always 1
}
const name = 'Ada';
const age = 36;
inspect`Hello ${name}, you are ${age} years old.`; // 1The Length Rule
There is always exactly one more string part than there are values. Each substitution sits between two string parts, even when one of those parts has nothing in it. If a substitution starts or ends the literal, or two substitutions are next to each other, JavaScript inserts an empty string '' to keep the count right.
function parts(strings, ...values) {
return { strings: [...strings], values };
}
parts`no substitutions`; // { strings: ['no substitutions'], values: [] }
parts`${1}${2}`; // { strings: ['', '', ''], values: [1, 2] }
parts`${'start'} end`; // { strings: ['', ' end'], values: ['start'] }Because of this rule, strings[i] always comes before values[i], and strings[i + 1] always comes after it. The next chapter uses that guarantee to rebuild the output.
Values Keep Their Types
An untagged template converts every substitution to a string. A tag does not. Each value arrives exactly as the expression evaluated, whether that is a number, an object, null, or a function.
function types(strings, ...values) {
return values.map((v) => (v === null ? 'null' : typeof v));
}
types`${42} ${'a'} ${{}} ${null} ${() => 1}`;
// [ 'number', 'string', 'object', 'null', 'function' ]Tags depend on this. A tag can treat a function value as a callback, keep an object intact, or decide for itself how to turn each value into a string.
Evaluation Order
All substitutions are evaluated left to right before the tag function runs. The tag never sees the expressions themselves, only their results.
const log = [];
const step = (n) => { log.push(n); return n; };
function tag(strings, ...values) {
log.push('tag');
return values;
}
tag`${step(1)} and ${step(2)}`;
console.log(log); // [ 1, 2, 'tag' ]Common Mistakes
| Mistake | What actually happens |
|---|---|
| Expecting `strings` to hold the values | `strings` holds only the static text between substitutions |
| Assuming values are already strings | Values keep their original types |
| Forgetting empty edge parts | A leading or trailing `${}` produces `''` in `strings` |
| Using `arguments.length` as the value count | `arguments` includes the strings array, so use `values.length` |
The strings array also has a raw property and some special identity rules. Later chapters cover both.
Exercise
For the call tag${a}${b} done, where a = 'x' and b = 7, what are strings and values inside the tag?
Show answer
strings is ['', '', ' done'] and values is ['x', 7]. The literal starts with a substitution, which gives a leading '', and the two substitutions are adjacent, which gives a second ''. There are three string parts for two values.
Exercise
A tag is called as ttotal: ${[1, 2]}. Is values[0] the string '1,2' or the array [1, 2]? Why?
Show answer
It is the array [1, 2]. A tag receives each substitution as its evaluated value without converting it to a string. Only an untagged template would turn it into '1,2'.