lukehoban/es6features

An interesting addition to the template strings section

softwarespot opened this issue · 1 comments

function valueToUpper(strings, ...values) {
    // Set the values i.e. first and last name to upper-case
    values[0] = values[0].toUpperCase();
    values[1] = values[1].toUpperCase();

    // Re-create the template
    return `${strings[0]}${values[0]}${strings[1]}${values[1]}${strings[2]}`;
}

// Create first and last name variables
let firstName = 'Joe';
let lastName = 'Bloggs';

// Pass the template to a custom format function
let template = valueToUpper`Welcome ${firstName} ${lastName} to the site.`;

// Diplsay the new template
console.log(template);

Probably worth pointing out that these are called Tagged Template Strings.