Skip to main content

Command Palette

Search for a command to run...

The Flashy Match Graphics: Mastering Template Literals in JS 📺

Updated
•3 min read•View as Markdown
The Flashy Match Graphics: Mastering Template Literals in JS 📺

This is the perfect way to kick off Season 2. If Season 1 was about learning to play the game, Season 2 is about playing with style and efficiency.

In the old days of JavaScript, joining text and variables together felt like trying to pass a ball through a crowded midfield—clunky and prone to errors. Template Literals are the "No-Look Pass" of modern JS: sleek, readable, and incredibly powerful.

1. The Problem: Traditional String Concatenation

Before ES6, if you wanted to create a dynamic sentence (like a player's stat line), you had to use the + operator. This is called Concatenation.

It’s a nightmare for managers because you have to manually manage spaces and quotes. One missed + or space, and your code breaks.

The "Old School" Way:

const name = "Cristiano Ronaldo"; const goals = 800; const club = "Al-Nassr";

const message = "The player " + name + " has scored " + goals + " goals for " + club + ".";

console.log(message); // One tiny mistake in the spaces above and the text looks like a mess!

2. The Solution: Template Literal Syntax

Template Literals use Backticks (the key below the Esc key: `) instead of single or double quotes. This tells JavaScript, "Prepare for a modern string!"

String Interpolation (The Magic ${})

Instead of breaking the string with + signs, you use a placeholder: ${variable}. This is called Interpolation.

The Modern Way:

const message = `The player \({name} has scored \){goals} goals for ${club}.`;

console.log(message);
// Clean, readable, and looks exactly like the final sentence!

3. Multi-line Strings: The Tactical Board

In the old days, if you wanted a string to span multiple lines, you had to use \n. With Template Literals, you just hit Enter. Whatever you type inside the backticks—including the line breaks—stays exactly as it is.

Scenario: Writing a post-match report.

const matchReport = `
--- MATCH REPORT ---
Player: ${name}
Status: MOTM
Goals: ${goals}
--------------------
`;

console.log(matchReport);

4. Modern Use Cases

Why do pro developers love this?

  • HTML Templates: It’s the easiest way to write HTML inside your JavaScript (perfect for building SaaS dashboards).

  • Math in Strings: You can even do calculations inside the ${}!

console.log(Next goal will be number ${goals + 1}!);

🏆 Daily Drill: The Match Graphic

Time to head to the console and upgrade your "Match Graphics":

  1. The Profile: Create variables for playerName, trophies, and isLegend (boolean).

  2. The Old Way: Try to create a sentence using + concatenation.

  3. The Template Way: Rewrite that sentence using backticks and ${}.

  4. The Multi-line: Create a 3-line "Player Card" string using a single pair of backticks.

Do you find it easier to read the code with backticks or the old + signs? Tell me in the comments!

More from this blog

JavaScript: The Champions League Edition

14 posts