Back to Course

Introduction to JavaScript

0% Complete
0/0 Steps
  1. JavaScript: Basics
    9Topics
    |
    10 Quizzes
  2. JavaScript: Variables
    9Topics
    |
    10 Quizzes
  3. JavaScript: Conditional Statements
    10Topics
    |
    11 Quizzes
  4. JavaScript: Functions
    10Topics
    |
    11 Quizzes
  5. JavaScript: Scope
    6Topics
    |
    7 Quizzes
  6. JavaScript: Arrays
    11Topics
    |
    12 Quizzes
  7. JavaScript: Loops
    8Topics
    |
    9 Quizzes
  8. JavaScript: High Order Functions
    3Topics
    |
    3 Quizzes
Lesson 2, Topic 8
In Progress

JavaScript: String Interpolation

Yasin Cakal 30 Oct 2021
Lesson Progress
0% Complete

We can use template literals in the ES6 version of JavaScript to insert or interpolate variables into strings. Take a look at the following example, which uses a template literal to combine strings:

const favFood= 'Pizza';
console.log(`My favourite food is ${favFood}.`);

//Output: My favourite food is Pizza.

Take note of the following:

  • Backticks ' enclose a template literal (this key is usually located on the top of your keyboard, left of the 1 key).
  • Inside the template literal there’s a placeholder, $favFood. In the template literal, the value of favFood is inserted.
  • The output of interpolating `My favourite food is ${favFood}.` is the string: My favourite food is Pizza.

The readability of the code is one of the biggest advantages of using template literals. You can more easily tell what the new string will be if you use template literals. You also don’t have to worry about escaping double or single quotes.

Responses

Leave a Reply

%d bloggers like this: