Skip to content

Latest commit

 

History

History
77 lines (56 loc) · 1.52 KB

File metadata and controls

77 lines (56 loc) · 1.52 KB

Section 09: Template Strings

The Template Strings

function getMessage() {
  const year = new Data().getFullYear();

  return `The year is ${year}.`;
}

getMessage();

The template strings use back-ticks symbol rather than the single or double quotes we're used to with regular strings. We can use template strings whenever we're using strings where we have to mix tons of different variables



[Exercise] Template Strings in Practice

Question

Refactor the function to use template strings

function doubleMessage(number) {
  return "Your number doubled is " + (2 * number);
}

Solution

function doubleMessage(number) {
  return `Your number doubled is ${2 * number}`;
}


[Exercise] Name Helpers

Question

Refactor the function to use template strings.

function fullName(firstName, lastName) {
  return firstName + lastName;
}

Solution

function fullName(firstName, lastName) {
  return `${firstName} ${lastName}`;
}