LESSON 06 · BEGINNER4 min read

Strings & Template Literals

Treat text as an immutable sequence, transform it with string methods, and build readable messages with template literals.

ES2024 / Node.js 20+Free to readModule 2 of 19
YOUR NEXT SMALL WINS

By the end, you’ll be able to…

  • Explain string immutability and the effect of string methods.
  • Use quotes, escapes, length, indexing, and slicing.
  • Transform text with includes, replace, trim, and case conversion.
  • Build messages with template literals and interpolation.

01Strings are immutable sequences

A string is an ordered sequence of characters. JavaScript strings are immutable: methods never change the original string. raw.trim() returns a new string with surrounding whitespace removed, while raw keeps its spaces. If you want the cleaned text later, assign the result to a name. This one rule explains many beginner surprises.

The length property counts UTF-16 code units, not user-perceived characters. For ordinary English text the difference rarely matters, but an emoji can occupy two code units. Treat length as a technical count and use it for limits and loops. Do not assume it equals the number of visible symbols in every language.

immutable.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.

02Quotes and escapes

Single quotes, double quotes, and backticks can all delimit a string. Choose one style and stay consistent. Inside a string, a backslash escapes the next character: it lets you include the same quote mark, a newline, or a literal backslash. Without the escape, the quote would end the string early.

Backticks have extra powers: they allow multi-line text and interpolation, which you will use shortly. For simple labels, ordinary quotes are enough. When a string contains both kinds of quotes, either escape one kind or switch delimiters. Readability matters more than saving a keystroke.

escapes.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.

03Index, slice, and search

Indexes start at zero. title[0] reads the first character, and title.at(-1) reads the last. The slice method takes a start index and an optional end index; the end is excluded. Negative indexes count from the end, so title.slice(-6) returns the final six characters.

Search methods answer questions. includes reports whether a substring appears, and it is case-sensitive. replace swaps the first match unless you use replaceAll. toUpperCase and toLowerCase change case, while trim removes whitespace at both ends. Each method returns a value; none edits the original string.

inspect-text.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.

04Template literals for readable messages

A template literal uses backticks and ${...} placeholders. The expression inside the braces is evaluated and converted to text. This is clearer than joining pieces with plus signs, especially when several values appear in one sentence. It also keeps the sentence's punctuation visible in the source.

Prefer building a message in a named constant when the same text is used more than once. Keep expressions small; calculate a value first if the interpolation becomes hard to read. A template literal produces a string, even when every placeholder holds a number.

template-message.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.

05Clean text in an order you can explain

Cleaning user text usually means trimming it and normalizing case before comparison. Chain the steps in the order you can explain: trim first, then lowercase, then test. Chaining is fine because each method returns a new string, but long chains can hide mistakes. Split a chain when a name would make the intent clearer.

Do not use replace to remove every space unless that is truly the requirement; trim only touches the edges. If you need to check whether an email contains an @ sign, includes is enough for a beginner check. Real validation is more involved, but the habit of explicit, readable transformations starts here.

normalize-email.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.
LET IT CLICK

The ideas worth keeping.

  • String methods return new values; the original string never changes.
  • Indexes and slices use zero-based positions, and the slice end index is excluded.
  • Template literals keep messages readable while inserting values directly.
TURN UNDERSTANDING INTO SOMETHING YOU CAN DO

Make it yours.

Type and test your code right here in the interactive workspace. Run your solution, check the output, and compare with the worked guide when ready.

01

Exercise 1: Clean and inspect a course code

Given raw = " js-101 ", create a trimmed uppercase code. Print the code, its length, its first and last characters, and whether it includes 101.

your_turn_1.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.
Explore a worked solution
one_possible_solution.jsJavaScript
const raw = "  js-101  ";const code = raw.trim().toUpperCase();console.log(code);console.log(code.length);console.log(code[0], code.at(-1));console.log(code.includes("101"));

trim removes the edge spaces before toUpperCase changes the letters. The cleaned code is JS-101, which is six characters long and still contains 101.

02

Exercise 2: Build a welcome message

Using name = "Ada", city = "Lagos", and lessons = 3, build one template literal with three lines: Welcome, Ada!, You are joining from Lagos., and 3 lessons are ready. Print the result with one console.log call.

your_turn_2.js⚡JavaScript
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.
Explore a worked solution
one_possible_solution.jsJavaScript
const name = "Ada";const city = "Lagos";const lessons = 3;const message = `Welcome, ${name}!You are joining from ${city}.${lessons} lessons are ready.`;console.log(message);

The template literal holds the line breaks and inserts each value at its placeholder. One console.log call prints the finished three-line string.

A SMALL QUESTION. A USEFUL NEXT STEP.

What stayed with you?

Check the ideas while they’re fresh. Your answers stay in this page and feedback appears as you go.

A LITTLE ACTIVE RECALL0 / 3 answered
01What does const cleaned = raw.trim() do to raw?
02What is "JavaScript".slice(0, 4)?
03What does includes return?
⚡CODING CHALLENGE
🔒Required to Mark Lesson Complete

Exercise 1: Clean and inspect a course code

Given raw = " js-101 ", create a trimmed uppercase code. Print the code, its length, its first and last characters, and whether it includes 101.

EXPECTED OUTPUT
JS-101
6
J 1
true
main.jsJavaScript
Ctrl + ↵
Execution Console
Click "Run & Verify" (or press Ctrl + Enter) to test your code against the challenge.

Keep exploring.

The official references behind this lesson, when you want a second angle or the full details.

One lesson closer.

Reading is a start. Trying it yourself makes the connection.

Solve the hands-on coding challenge in the knowledge section to verify your code and unlock lesson completion.