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 questionsYou 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]); // t10
J
t2Explore
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.
const word = "JavaScript";
console.log(word[0]);
console.log(word.slice(0, 10));J
JavaScript3Problem
The second letter
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)
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.
const s = " Hello, Mina ";const t = s.trim();console.log(t.toUpperCase());console.log(t.includes("Mina"), t.length);console.log(s.length);
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.
console.log("5" + 3);537Problem
"5" + 3
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
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: 15Mina is 14 years old.
Next year: 1510Practice
A sentence from two variables
Use a template literal so the program prints Mina is 14 years old. using the two variables.
Mina is 14 years old.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.
HELLO!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');SyntaxError: missing ) after argument list
- The word before the colon names the kind of error: ReferenceError, TypeError, SyntaxError.
- After the colon: what went wrong, usually with the name involved.
- 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());TypeError: s.toUppercase is not a function (line 2)
- The word before the colon names the kind of error: ReferenceError, TypeError, SyntaxError.
- After the colon: what went wrong, usually with the name involved.
- 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`.