Template Literals
Part of the Logic & Flow section of Coddy's JavaScript journey — lesson 3 of 65.
` 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. You can also include symbols like $ by placing them directly inside the backticks.Example:const userName = "Sam";
const price = 5;
const message = `Welcome, ${userName}! Your total is ${price}.`;
// Inserts userName and price inside the string\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`;Challenge
EasyCreate 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.
Try it yourself
function greetAll(names) {
// Write your code here
}
// Do not write anything outside functionThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Strings In Depth
String FundamentalsIterate Over StringsTemplate LiteralsString MethodsRecap - String Weaver4JSON Part 2
Iterate Over JSONNested JSONJSON Optional ChainingShallow And Deep CopyRecap - Bicycle ShopRecap - Solar System10Manage Festival System
Project OverviewAdd Movies & Venues2Multi-dimensional Arrays
2D Arrays BasicsAccessing 2D Array ElementsNested Loops with 2D ArraysRecap - 2D ArraysMatrix Addition & SubstractionJagged Arrays3D Arrays And BeyondCommon 2D Array PatternsRecap - All About Arrays5Sets Part 1
What Is A Set?Iterating Over SetsAdding An ElementRemoving An ElementChecking If An Element ExistsSize And Is EmptyCopy And ClearRecap - Basic Of Sets8Arrays Interesting Topics
Array DestructuringSpread Syntax in ArraysSparse ArraysRecap - Arrays Workshop3JSON Part 1
What is a JSON?Check If Key ExistsObject MethodsThe Spread Operator Part 1The Spread Operator Part 2Remove KeysRecap - JSON Manipulate Keys