Skip to content

OutputNot attempted

Tagged template literals

function tag(strings, ...values) {
console.log(strings);
console.log(values);
return strings.raw.length;
}
const person = 'Lydia';
const age = 21;
console.log(tag`${person} is ${age} years old`);

Spaces and quote style don’t matter · write a thrown error as TypeError

Ctrl Enter
Show answer & explanation

The expected output comes from actually running the snippet; opening this shows it next to the explanation.

A tag receives the static string parts first (always one more than the number of expressions, here an empty string before ${person}), then each interpolated value. This is how styled-components and GraphQL's gql work.