edubun
English
1 / 13

5 / 12 · Intro

Strings and template literals

"5" + 3 gives "53", not 8. When one side of + is a string, JavaScript joins text instead of adding numbers.

Jump to the 11 practice questions

You will learn

  • Read a string's length, characters and slices
  • Use string methods and predict what + does with text
  • Build sentences with template literals

Before this: Numbers and math

1Explain

Length and positions

Every string knows its length: "JavaScript".length is 10. For simple Latin text this equals the number of characters. Some emoji count as 2, because length counts code units (the small pieces the text is stored in). Each character also has a position, called its index, and counting starts at 0, not 1. So in "JavaScript", index 0 is J, index 4 is S and the last index is 9, one less than the length.

const word = "JavaScript";
console.log(word.length);                // 10
console.log(word[0]);                    // J
console.log(word[word.length - 1]);      // t
Output
10
J
t

2Explore

Index and slice

word[i] is one character; slice(start, end) stops just before end. Past the last index you get undefined or an empty string.

start
end
const word = "JavaScript";
console.log(word[0]);
console.log(word.slice(0, 10));
Output
J
JavaScript

3Problem

The second letter

What does this print?
const s = "Seoul";
console.log(s[1]);

Hint

Index 0 is S.

Solution

Answer: e

Index 0 is S, index 1 is e.

4Problem

slice(4, 10)

What does this print?
console.log("JavaScript".slice(4, 10));

Hint

Count J-a-v-a as 0 to 3; index 4 starts the slice.

Solution

Answer: Script

Index 4 is S; slice stops just before index 10, the end.

5Explain

String methods return new strings

Strings come with built-in tools called methods, which you call with a dot after the string. toUpperCase() and toLowerCase() return a copy in capitals or small letters. includes("Script") answers true or false depending on whether the text appears inside.

Watch s and t: trim() makes a new string without the outer spaces, and s keeps them.

  1. const s = " Hello, Mina ";
  2. const t = s.trim();
  3. console.log(t.toUpperCase());
  4. console.log(t.includes("Mina"), t.length);
  5. console.log(s.length);
Variables
Output
HELLO, MINA
true 11
15

6Explore

+ with text and numbers

The + operator joins strings end to end, which is called concatenation. "Hello, " + "Mina" gives "Hello, Mina". Spaces are not added for you, so put them inside the quotes yourself. Here is the trap from the lead: if either side of + is a string, JavaScript turns the other side into a string and joins them. "5" + 3 is "53". The other arithmetic operators do not join text, so "5" * 2 converts "5" to a number and gives 10.

When either side of + is a string, + joins text. Other operators such as * turn the string into a number first.

expression
console.log("5" + 3);
Output
53

7Problem

"5" + 3

What does this print?
console.log("5" + 3);

Hint

One side is a string.

Solution

Answer: 53

One side is a string, so + joins: "5" + "3" is "53".

8Problem

Left to right

What does this print?
console.log(1 + 2 + "3");

Hint

1 + 2 happens first, before the string appears.

Solution

Answer: 33

Left to right: 1 + 2 is the number 3, then 3 + "3" joins to "33".

9Explain

Template literals

Building a sentence with + gets messy fast: name + " is " + age + " years old." needs careful spaces and quotes. A template literal, written with backticks, lets you drop values straight into the text with ${ }. Whatever is inside the braces is evaluated as an expression and the result is placed into the string. Template literals can also span several lines.

const name = "Mina";
const age = 14;
console.log(`${name} is ${age} years old.`);   // Mina is 14 years old.
console.log(`Next year: ${age + 1}`);           // Next year: 15
Output
Mina is 14 years old.
Next year: 15

10Practice

A sentence from two variables

Use a template literal so the program prints Mina is 14 years old. using the two variables.

Expected output
Mina is 14 years old.
Your code
const name = "Mina";
const age = 14;
console.log("name is age years old.");
Hint

Use backticks, not double quotes, and keep the full stop at the end.

A solution
const name = "Mina";
const age = 14;
console.log(`${name} is ${age} years old.`);

11Practice

Shout it

Print HELLO!: the word in capitals with an exclamation mark joined on the end.

Expected output
HELLO!
Your code
const quiet = "hello";
console.log(quiet);
Hint

toUpperCase needs its brackets: quiet.toUpperCase(). Then join "!".

A solution
const quiet = "hello";
console.log(quiet.toUpperCase() + "!");

12Explain

Common mistakes

console.log('It's late');
Error message
SyntaxError: missing ) after argument list
  1. The word before the colon names the kind of error: ReferenceError, TypeError, SyntaxError.
  2. After the colon: what went wrong, usually with the name involved.
  3. The line number shows where the program stopped. A SyntaxError stops it before any line runs.

The apostrophe ends the single-quoted string early. Wrap the text in double quotes instead: "It's late".

const s = "hi";
console.log(s.toUppercase());
Error message
TypeError: s.toUppercase is not a function (line 2)
  1. The word before the colon names the kind of error: ReferenceError, TypeError, SyntaxError.
  2. After the colon: what went wrong, usually with the name involved.
  3. The line number shows where the program stopped. A SyntaxError stops it before any line runs.

Method names are case sensitive: the method is toUpperCase, with a capital C.

13

Summary

  • Strings use "double", 'single' or `backtick` quotes.
  • Indexes start at 0; the last index is length - 1.
  • Methods like toUpperCase, includes, slice and trim return new strings.
  • If either side of + is a string, + joins text.
  • Template literals: `text ${expression} text`.

Next unitBooleans and comparisons
Full text · The whole lesson as one text

Text in a program

"Seoul" is a string that a program can use as a city name. Names, messages, addresses, passwords and whole web pages are all text, and JavaScript stores text as strings. A string is a sequence of characters between quotes. JavaScript accepts three kinds of quotes: double quotes "like this", single quotes 'like this', and backticks `like this`. The first two behave the same. Backticks make a template literal, which has extra powers covered below.

If your text contains one kind of quote, wrap it in the other: "It's late" works because the apostrophe is not a double quote. You can also put a backslash before a quote to say "this one is part of the text": 'It\'s late'. A backslash followed by n, written \n, means a line break inside the string.

Length and positions

Every string knows its length: "JavaScript".length is 10. For simple Latin text this equals the number of characters. Some emoji count as 2, because length counts code units (the small pieces the text is stored in). Each character also has a position, called its index, and counting starts at 0, not 1. So in "JavaScript", index 0 is J, index 4 is S and the last index is 9, one less than the length. You read a single character with square brackets: word[0]. Starting from 0 feels odd at first, but most programming languages count this way, and you will see it again with arrays.

const word = "JavaScript";
console.log(word.length);                // 10
console.log(word[0]);                    // J
console.log(word[word.length - 1]);      // t

Useful string methods

Strings come with built-in tools called methods, which you call with a dot after the string. toUpperCase() and toLowerCase() return a copy in capitals or small letters. includes("Script") answers true or false depending on whether the text appears inside. slice(0, 4) returns the characters from index 0 up to, but not including, index 4, which in "JavaScript" is "Java". trim() removes spaces from both ends, handy for cleaning up something a person typed. None of these change the original string. Strings cannot be changed in place. Methods such as slice and trim hand back a new string, while includes hands back true or false.

Joining strings with +

The + operator joins strings end to end, which is called concatenation. "Hello, " + "Mina" gives "Hello, Mina". Spaces are not added for you, so put them inside the quotes yourself. Here is the trap from the lead: if either side of + is a string, JavaScript turns the other side into a string and joins them. "5" + 3 is "53". The other arithmetic operators do not join text, so "5" * 2 converts "5" to a number and gives 10. To turn a string that holds digits into a real number, use Number("5").

Template literals

Building a sentence with + gets messy fast: name + " is " + age + " years old." needs careful spaces and quotes. A template literal, written with backticks, lets you drop values straight into the text with ${ }. Whatever is inside the braces is evaluated as an expression and the result is placed into the string. Template literals can also span several lines.

const name = "Mina";
const age = 14;
console.log(`${name} is ${age} years old.`);   // Mina is 14 years old.
console.log(`Next year: ${age + 1}`);           // Next year: 15

Worked example. A shop label needs to read "3 apples cost 4.5". With const n = 3, price = 1.5; the template `${n} apples cost ${n * price}` does it in one line, and the multiplication happens inside the braces.

Backticks around the text, dollar sign and braces around each value: `Hi ${name}`

Key points
  • Strings use "double", 'single' or `backtick` quotes.
  • Indexes start at 0; the last index is length - 1.
  • Methods like toUpperCase, includes, slice and trim return new strings.
  • If either side of + is a string, + joins text.
  • Template literals: `text ${expression} text`.

Practice questions from Strings and template literals on edubun

Embed this quiz

Free to embed. Please keep the credit line.

Sources for this course (2)
  • MDN Web Docs, JavaScript Guide (developer.mozilla.org/en-US/docs/Web/JavaScript/Guide, chapters Introduction, Grammar and types, Control flow, Loops and iteration, Functions, Expressions and operators, Numbers and strings, Indexed collections, Working with objects; checked 2026-10-17)
  • Ecma International, ECMA-262 ECMAScript 2026 Language Specification, 17th edition, June 2026 (tc39.es/ecma262 and ecma-international.org/publications-and-standards/standards/ecma-262, checked 2026-10-17).

Checked: 2026-10-17