tag function

tag function's first argument has a special raw property, allows you to access the raw strings as entered, without processing escape sequences.

// ⭐️ tag function (template function)
function raw(strings) {
    // ⭐️ first argument (`strings`) has a special `raw` property
    return strings.raw;
}

// ⭐️ tagged templates
raw`line 1 \n line 2`,          // [ 'line 1 \\n line 2' ] (`\n` not escaped)
raw`5 = \n ${2 + 3}!`,          // [ '5 = \\n ', '' ] (`\n` not escaped)

// ⭐️ raw strings
String.raw`5 = \n ${2 + 3}!`,   // '5 = \\n 5!'
String.raw`5 = \n \${2 + 3}!`,   // '5 = \\n \\${2 + 3}!'

// ⭐️ default template function
`5 = \n \${2 + 3}!`,            // 5 = (new line)
                                //  ${2 + 3}!

Last updated