Menu
Coddy logo textTech

Template Literals

Part of the Logic & Flow section of Coddy's JavaScript journey — lesson 3 of 65.

Template literals use backticks `` to create strings and allow you to insert expressions using ${}.  For instance, when creating greetings, you can dynamically embed names or other data directly into the template literal.

Example:

const userName = "Sam";
const message = `Welcome, ${userName}!`;
// Inserts userName inside the string

Template literals also support multi-line strings without needing the \n escape character. You can simply press Enter for a new line, and the line break will be preserved in the output. Both approaches work:

// Using \n
const message1 = `Line 1\nLine 2`;

// Using actual line breaks
const message2 = `Line 1
Line 2`;

Both produce the same result

challenge icon

Challenge

Easy

Create a function named greetAll that takes an array of names and returns one string. Each name in the array should produce a line with the format Hello, <Name>! joined together into a single string, separated by newlines. Use template literals for the greeting lines.

Cheat sheet

Template literals use backticks `` to create strings and allow you to insert expressions using ${}:

const userName = "Sam";
const message = `Welcome, ${userName}!`;
// Inserts userName inside the string

Template literals support multi-line strings without needing \n escape characters:

// Using \n
const message1 = `Line 1\nLine 2`;

// Using actual line breaks
const message2 = `Line 1
Line 2`;
// Both produce the same result

Try it yourself

function greetAll(names) {
  // Write your code here
}
// Do not write anything outside function
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow