Strings and Arrays
- 5 min reading
- 6 min activity
0 of 5 tasks doneup to 95 XP
Mission 4, step 4. Most of what people see on a website is text. Today you shape text and keep many values in one list.
- Measure, change and search text with string methods.
- Build a message with a template literal.
- Make an array, read and change its items, and count them.
In lesson 4.3 you met the operator %. One quick question.
Quick recall
Tools for text
Every string has ready-made tools. .length is a property, so it has no brackets. The others are methods: they do something and need ().
| Code | Result |
|---|---|
"Hello world".length (spaces count too) | 11 |
"Lena".toUpperCase(), toLowerCase() the opposite | "LENA" |
"Learning JavaScript is fun.".indexOf("fun") (from 0; -1 = not found) | 23 |
"2026-01-20".slice(0, 4) (from 0 up to 4, without 4) | "2026" |
"apple apple".replace("apple", "pear") (first match only; replaceAll() changes all) | "pear apple" |
On the slice() slide, console.log(year); stands inside the comment, so nothing prints. After //, the rest of the line is a comment: give every statement its own line. Some languages have their own capitals: for Turkish, use toLocaleUpperCase("tr-TR").
Code and result
Template literals
A template literal is text in backticks. Inside it, ${ } puts a value into the text, without + and many quotes. On a phone keyboard, the backtick is often on the second page of symbols.
let name = "Ayla";
let age = 25;
console.log("My name is " + name + " and I am " + age + ".");
console.log(`My name is ${name} and I am ${age}.`); // the same text
Worked example: a welcome panel
Eleni Demir logs in to a learning site. The site gets her data in small letters and builds a friendly welcome.
- Tidy the data
firstName.toUpperCase()turns "eleni" into "ELENI". The same for the last name and the city. - Build the message
One template literal puts the values into the text. It can go over several lines.
- Count
${first.length + last.length}adds the letters of both names: 5 + 5 = 10.
The slide calls this number the length of "your user name", but it counts first name plus last name.
Welcome panel
The daily application from the course slides: tidy the data, then build the welcome message. Choose the right piece for every gap.
Arrays
An array keeps many values in one box, in square brackets. Each item has a number, its index. Counting starts at 0.
let fruits = ["Apple", "Pear", "Banana", "Strawberry"];
console.log(fruits[0]); // Apple
fruits[1] = "Kiwi"; // Pear is gone, Kiwi is in its place
console.log(fruits.length); // 4
let mixed = ["Ayla", 25, true, null]; // types can mix
Counting from 0
Fruit list
Slides 45 to 48 of the course in one exercise: read, change and count the items of an array.
- Method
- A ready-made tool that does something, like
toUpperCase(). - Property
- A value that belongs to something, like
.length. No brackets. - Template literal
- Text in backticks with values inside
${ }. - Array and index
- A list in square brackets; the index is the number of an item, from 0.
In short
.lengthcounts,toUpperCase()changes letters,indexOf()searches,slice()cuts,replace()changes.- A template literal puts values into text:
`Hello, ${name}!`. fruits[0]is the first item;fruits.lengthis the number of items.
Your code makes decisions: if this, then that.
Sources
- MDN Web Docs (2026). Useful string methods.
- MDN Web Docs (2026). Template literals (Template strings).
- MDN Web Docs (2026). Arrays.
- MDN Web Docs (2026). String.prototype.toLocaleUpperCase().
End of the lesson
This lesson: 0 of 105 XP
Code Cards in this lesson: 8
.lengthtoUpperCase()indexOf()slice()replace()`${ }`arrayarray[i]