Skip to content
Algorithms for Beginners

Learn / Lesson 4 of 10

Strings

Work with text one character at a time.

Words on this page, in plain English (10)Open if any word is new to you
Variable
A labelled box that holds a value. You give it a name, put something in it, and use the name later to get it back.
String
A piece of text, written between quotes. Each letter in it is a character.
Boolean (true or false)
A value that can only be true or false. Like a light switch: on or off.
Array (list)
A list of values kept in order, inside square brackets. Like a row of numbered lockers.
Index (position)
Where an item sits in a list. Counting starts at 0, not 1, so the first item is at position 0.
Length
How many items a list (or letters a string) has. The last position is always length - 1.
Loop
Code that repeats. A for loop repeats once per item or a set number of times; a while loop repeats as long as something stays true.
console.log
Prints something so you can see it. The easiest way to find out what your code is really doing.
Pattern
A trick that solves a whole family of problems. Learn the trick once, and you can solve problems you've never seen.
In place
Changing the list you were given, instead of making a new one. Saves memory.

Every word, explained simply

Text is a list of characters

A string is text: letters, digits, spaces, symbols. In many ways it behaves like an array of characters. It has a length, and each character has a position, starting at 0:

Try it: characters

const word = "pattern";
console.log(word.length);
console.log(word[0]);
console.log(word[word.length - 1]);

for (const letter of word) {
console.log(letter);
}

Press Run to see the output. You can change the code first.

Strings can't be changed in place

Unlike arrays, you can't change one character of a string: word[0] = "b" doesn't work (try it below: it's an error). Instead, every string operation gives you a new string and leaves the original alone:

Try it: strings stay the same

const word = "cat";
const shout = word.toUpperCase();
console.log(shout, word);  // a new string; the original is unchanged

word[0] = "b"; // not allowed: strings can't be changed

Press Run to see the output. You can change the code first.

Useful string tools

CodeWhat it does
text.lengthhow many characters
text.toLowerCase(), toUpperCase()change case
text.includes("ab")true if "ab" appears in it
text.indexOf("ab")where "ab" starts, or -1
text.slice(a, b)the characters from a up to (not including) b
text.split(" ")cut into an array at every space
list.join("-")glue an array of strings with - between
text.trim()remove spaces from both ends

Try it: a few tools

const sentence = "  The quick brown fox  ";
const clean = sentence.trim();
console.log(clean);
console.log(clean.toLowerCase());
console.log(clean.slice(4, 9));

const words = clean.split(" ");
console.log(words);
console.log(words.join("_"));

Press Run to see the output. You can change the code first.

Turning text into an array and back

Since strings can't change, a common trick is: turn the string into an array of characters, change the array, and join it back.

Try it: reverse a word

const word = "stressed";
const letters = [...word];        // ["s", "t", "r", ...]
letters.reverse();
console.log(letters.join(""));

Press Run to see the output. You can change the code first.

[...word] spreads the string into an array of its characters. join("") glues them back with nothing in between.

Characters are numbers inside

The computer stores each character as a number, its code. charCodeAt reads it. Letters a to z have codes in a row, which is a handy trick: subtracting the code of "a" turns "a" into 0, "b" into 1, and so on.

Try it: character codes

console.log("a".charCodeAt(0), "b".charCodeAt(0), "z".charCodeAt(0));
console.log("c".charCodeAt(0) - "a".charCodeAt(0));   // c is letter number 2

Press Run to see the output. You can change the code first.

Comparing strings

=== checks whether two strings have exactly the same characters. Capitals count: "Cat" === "cat" is false. To ignore case, compare lowercase versions.

Try it: comparing

console.log("cat" === "cat");
console.log("Cat" === "cat");
console.log("Cat".toLowerCase() === "cat");

Press Run to see the output. You can change the code first.

Your turn

Given name = "ada lovelace", show it with the first letter of each word as a capital: Ada Lovelace. (Hint: split into words, fix each word, join them back.)

Your turn

const name = "ada lovelace";

Press Run to see the output. You can change the code first.

What you learned

  • A string is like a list of characters: length, positions from 0, for...of.
  • Strings never change; every operation makes a new string.
  • split and join move between strings and arrays.
  • Characters have number codes, which makes letter tricks possible.