JavaScript Template Literals Advanced (Tagged Templates) Quiz
Answer 35 questions digging into template literal basics, tagged function mechanics, string/value processing, security sanitization, custom formatting, localization, DSL construction, and performance trade-offs.
Question 1
Which feature do template literals add beyond single or double quotes?
Question 2
Why are template literals convenient for multi-line strings?
Question 3
What logs?
const user = 'Nia'
console.log(`Hello, ${user}!`)Question 4
What is a tagged template literal?
Question 5
What arguments does a tag function receive?
Question 6
What logs?
function tag(strings, value) {
console.log(strings[0], value)
}
const amount = 5
tag`Items: ${amount}`Question 7
Which property exposes unescaped characters inside a tag?
Question 8
Why should tag functions treat strings as immutable?
Question 9
What logs?
function repeatTag(strings, value) {
return `${strings[0]}${value}${value}`
}
console.log(repeatTag`Value: ${3}`)Question 10
Why can tag functions return non-string values?
Question 11
What logs?
const toObject = (strings, ...values) => ({ strings, values })
const result = toObject`A ${1} B ${2}`
console.log(result.values)
console.log(result.strings.length)Question 12
How does a tag differentiate between segments and values when reconstructing output?
Question 13
Why are tagged templates popular for HTML sanitization?
Question 14
What logs?
const escapeHTML = (strings, ...values) => strings.reduce((out, str, i) => {
const val = values[i] ?? ''
return out + str + val.replace?.(/[<>&"]/g, c => ({ '&': '&', '<': '<', '>': '>', '"': '"' }[c])) ?? val
}, '')
const unsafe = '<script>1</script>'
console.log(escapeHTML`Safe: ${unsafe}`)Question 15
Why should security tags carefully handle non-string values?
Question 16
Which practice strengthens sanitized tagged templates?
Question 17
What logs?
const currency = (strings, value) => `${strings[0]}$${value.toFixed(2)}`
console.log(currency`Price: ${4.5}`)Question 18
How do tagged templates simplify custom string builders?
Question 19
What logs?
function pad(strings, value, width = 4) {
const num = String(value)
return `${strings[0]}${num.padStart(width, '0')}`
}
console.log(pad`ID: ${42}`)Question 20
Why can tagged templates improve readability in logging utilities?
Question 21
Why are tagged templates useful for localization?
Question 22
What logs?
const i18n = (strings, ...values) => strings.reduce((acc, part, i) => acc + part + (values[i]?.toLocaleString?.('de-DE') ?? values[i] ?? ''), '')
console.log(i18n`Total: ${1234.5}`)Question 23
How can localization tags handle pluralization?
Question 24
Why should localized tags avoid concatenating raw user input?
Question 25
What is a DSL (domain-specific language) in the context of tagged templates?
Question 26
What logs?
const sql = (strings, ...values) => ({
text: strings.reduce((acc, part, i) => acc + part + (values[i] ? '$' + (i + 1) : ''), ''),
values
})
const query = sql`SELECT * FROM users WHERE id = ${7}`
console.log(query.text)
console.log(query.values)Question 27
Why should DSL tags validate their input segments?
Question 28
What logs?
const css = (strings, ...values) => strings.reduce((out, str, i) => out + str + (values[i] ?? ''), '').trim()
console.log(css`
color: ${'red'};
font-weight: ${'bold'};
`)Question 29
Why can tagged templates be slower than plain template literals?
Question 30
What logs?
let calls = 0
const countTag = (strings, ...values) => {
calls += 1
return strings.join('') + values.join('')
}
countTag`A ${1}`
countTag`B ${2}`
console.log(calls)Question 31
How can developers mitigate tagged template overhead?
Question 32
Why should large tagged templates avoid allocating new arrays each run?
Question 33
What logs?
const highlight = (strings, ...values) => strings.reduce((out, str, i) => out + str + (values[i] ? `<mark>${values[i]}</mark>` : ''), '')
console.log(highlight`Result: ${'OK'}!`)Question 34
Why should performance-sensitive tags avoid heavy regex work per call?
Question 35
When are tagged templates most compelling?
